GridView更新时出错 [英] error upon gridview updating

查看:74
本文介绍了GridView更新时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,..我是asp.net,gridview的学习者.我需要澄清一下,这是代码...

hi all,.. i am a learner of asp.net, gridview. i need some clarification and here is the code...

Imports System.Data.OleDb
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim cons As String
    Dim mycon As OleDbConnection
    Dim mycmd As OleDbCommand
    Dim myadapter As OleDbDataAdapter
    Dim myset As New Data.DataSet
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            cons = "Provider=SQLOLEDB;Data Source=user;Integrated Security=SSPI;Initial Catalog=db"
            mycon = New OleDbConnection(cons)
            mycon.Open()
            If Not IsPostBack Then
                fill()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox(ex.StackTrace)
        End Try
    End Sub
    Sub fill()
        myadapter = New OleDbDataAdapter("select * from details", mycon)
        myadapter.Fill(myset, "details")
        GridView1.DataSource = myset.Tables("details")
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        fill()

    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        fill()

    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Try
            Dim i As Integer
            i = e.RowIndex
            Dim trollno As String
            trollno = GridView1.Rows(i).Cells(0).Text
            Dim rows As GridViewRow
            rows = GridView1.Rows(i)
            

            Dim tname, tage, tgender, thobbies, tdept, tcourse As String
            'Dim tgender As RadioButtonList
            'Dim thobbies As CheckBoxList
            'Dim tdept, tcourse As DropDownList
            tname = CType(rows.Cells(1).Controls(0), TextBox).Text
            tage = CType(rows.Cells(2).Controls(0), TextBox).Text
            tgender = CType(rows.Cells(3).Controls(0), RadioButtonList).Text
            thobbies = CType(rows.Cells(4).Controls(0), CheckBoxList).Text
            tdept = CType(rows.Cells(5).Controls(0), DropDownList).Text
            tcourse = CType(rows.Cells(6).Controls(0), DropDownList).Text
      

            mycmd = New OleDbCommand("gvIUDGS", mycon)
            mycmd.CommandType = Data.CommandType.StoredProcedure
            mycmd.Parameters.AddWithValue("@choice", "update")
            mycmd.Parameters.Add("@rollno", OleDbType.Integer).Value = trollno
            mycmd.Parameters.Add("@name", OleDbType.VarChar, 50).Value = tname
            mycmd.Parameters.Add("@age", OleDbType.Integer).Value = tage
            mycmd.Parameters.Add("@gender", OleDbType.VarChar, 50).Value = tgender
            mycmd.Parameters.Add("@hobbies", OleDbType.VarChar, 50).Value = thobbies
            mycmd.Parameters.Add("@dept", OleDbType.VarChar, 50).Value = tdept
            mycmd.Parameters.Add("@course", OleDbType.VarChar, 50).Value = tcourse
            mycmd.ExecuteNonQuery()
            MsgBox("1 record updated successfuly")
            GridView1.EditIndex = -1
            fill()
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox(ex.StackTrace)
        End Try
        
    End Sub
End Class


执行此代码后,在更新过程中出现错误无法将类型为"system.web.UI.LiteralControl"的对象转换为"system.web.UI.WebControls" .textbox.有人可以解释为什么这发生异常了吗?!??并且应该对此做出什么更改?


相应的源代码在这里:


upon executing this code, during update i got an error as "unable to cast object of type ''system.web.UI.LiteralControl'' to ''system.web.UI.WebControls''.textbox. Can anyone explain why this exception occured?!!? and what change should be made to correcr this??


THE CORRESPONDING SOURCE CODE IS HERE:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="2" Style="position: relative; left: 31px; top: 100px;" AutoGenerateColumns="False">
        <Columns >
        <asp:TemplateField HeaderText ="Rollno">
        <ItemTemplate><%#Eval("rollno")%></ItemTemplate>
        <EditItemTemplate >
        <asp:Label ID="gvrollno" runat ="server" Text ='<%#Eval("rollno")%>'></asp:Label>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Name" >
        <ItemTemplate ><%#Eval("name")%></ItemTemplate>
        <EditItemTemplate >
        <asp:TextBox ID="gvname" runat ="server" Text='<%#Eval("name")%>'>
        </asp:TextBox>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Age" >
        <ItemTemplate ><%#Eval("age")%></ItemTemplate>
        <EditItemTemplate >
        <asp:TextBox ID="gvage" runat="server" Text='<%#Eval("age")%>'></asp:TextBox>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Gender">
        <ItemTemplate ><%#Eval("gender")%></ItemTemplate>
        <EditItemTemplate>
        <asp:RadioButtonList ID="rbgender" runat ="server">
       <asp:ListItem value="male" Text ="male"></asp:ListItem>
       <asp:ListItem Value ="female" Text ="female"></asp:ListItem>
     </asp:RadioButtonList>
       </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Hobbies">
        <ItemTemplate> <%#Eval("hobbies")%> </ItemTemplate>
        <EditItemTemplate >
        <asp:CheckBoxList id="cbhobbies" runat ="server">
       <asp:ListItem value="cricket">Cricket</asp:ListItem>
       <asp:ListItem Value="singing">Singing</asp:ListItem>
       <asp:ListItem Value="playing">Playing</asp:ListItem>
       <asp:ListItem Value="painting">Painting</asp:ListItem>
       <asp:ListItem Value ="reading">Reading</asp:ListItem>
        </asp:CheckBoxList>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Department" >
       <ItemTemplate><%#Eval("department")%></ItemTemplate>
        <EditItemTemplate >
        <asp:DropDownList id="ddldept" runat ="server" >
        <asp:ListItem Value ="IT">IT</asp:ListItem>
        <asp:ListItem Value ="CSE">CSE</asp:ListItem>
        <asp:ListItem Value ="MECH">MECH</asp:ListItem>
        <asp:ListItem Value ="ECE">ECE</asp:ListItem>
        </asp:DropDownList>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Course" >
        <ItemTemplate ><%#Eval("course")%></ItemTemplate>
        <EditItemTemplate >
        <asp:DropDownList ID="ddlcourse" runat ="server" >
        <asp:ListItem Value ="b.e">B.E</asp:ListItem>
        <asp:ListItem Value ="b.tech">B.Tech</asp:ListItem>
        </asp:DropDownList>
        </EditItemTemplate>
        </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />

        </Columns>
        </asp:GridView>
        &nbsp; &nbsp;</div>
    </form>
</body>
</html>



及其存储的过程是:



AND ITS STORED PROCEDURE IS:

ALTER procedure [dbo].[gvIUDGS]
(@rollno int,@name varchar(50),@age int,@gender varchar(50),@hobbies varchar(50),@department varchar(50),@course varchar(50),@choice varchar(50))
as
begin
if(@choice='insert')
insert into details values(@rollno,@name,@age,@gender,@hobbies,@department,@course)
if(@choice='update')
update details set name=@name,age=@age,gender=@gender,hobbies=@hobbies,department=@department,course=@course where rollno=@rollno
if(@choice='delete')
delete from details where rollno=@rollno
if(@choice='get')
select * from details
if(@choice='select')
select * from details where rollno=@rollno
end

推荐答案

据我所知,这没有任何问题您显示给我们的代码.
过去,当我在Web窗体上拥有一个TextBox控件,然后将其更改为Literal时,却发生了这种情况,但id保持不变.设计器文件并不总是更新为正确的控件类型,因此我的建议是检查设计器文件中的Literal实例,并查看这些声明之一是否不正确.这将帮助您了解发生此问题的原因.
要解决此问题,最简单的方法是将有问题的文本框重命名为新名称.这将导致Studio正确清理设计器文件.
From what I can see, there isn''t any issue in the code you have shown us.
I have had this happen in the past when I have a TextBox control on a web form and then I change it to a Literal, but leave the id the same. The designer file does not always update to the correct control type, so my advice would be to check the designer file for instances of Literals and see if one of these declarations is incorrect. This will help you to understand why the issue occured.

To fix the issue, the easiest way would be to rename the textbox in question to a new name. That should cause Studio to clean up the designer file properly.


这篇关于GridView更新时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆