主题:datagrid中“未将对象引用设置到对象的实例"这一错误怎么解决?
使用了一个datagrid,并添加了编辑更新取消和删除列,可是只有编辑和取消能用,当使用更新和删除时,就出现未将对象引用设置到对象的实例这一提示,是怎么回事?代码如下:
Imports System.Data.SqlClient
Public Class dkxwxmgl
Inherits System.Web.UI.Page
Public Shared a As Integer
Dim ds As New DataSet
Public da As SqlDataAdapter
Dim conn As SqlConnection
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
conn = New SqlConnection("data source=localhost;integrated security=true;initial catalog=zixin")
da = New SqlDataAdapter("select * from 贷款行为细目表", conn)
da.FillSchema(ds, SchemaType.Source, "贷款行为细目表")
da.Fill(ds, "贷款行为细目表")
If Not Page.IsPostBack Then
dkxmgrid.DataSource = ds
dkxmgrid.DataMember = "贷款行为细目表"
dkxmgrid.DataBind()
End If
End Sub
Private Sub dkxmgrid_DeleteCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dkxmgrid.DeleteCommand
a = CInt(e.Item.Cells(2).Text)
Dim rowDelete As DataRow = ds.Tables("贷款行为细目表").Rows.Find(a)
rowDelete.Delete()
dkxmgrid.DataSource = ds.Tables("贷款行为细目表")
dkxmgrid.DataBind()
Dim myCBuilder As New SqlCommandBuilder(da)
da.DeleteCommand = myCBuilder.GetDeleteCommand
'da.InsertCommand = myCBuilder.GetInsertCommand
'da.UpdateCommand = myCBuilder.GetUpdateCommand
da.Update(ds, "贷款行为细目表")
End Sub
Private Sub dkxmgrid_UpdateCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dkxmgrid.UpdateCommand
Dim rowChange As DataRow = ds.Tables("贷款行为细目表").Rows.Find(a)
rowChange.BeginEdit()
rowChange("细目名称") = (CType(e.Item.Cells(3).Controls(0), TextBox)).Text
rowChange("分值") = (CType(e.Item.Cells(4).Controls(0), TextBox)).Text
rowChange("打分类型") = (CType(e.Item.Cells(5).Controls(0), TextBox)).Text
rowChange.EndEdit()
dkxmgrid.EditItemIndex = -1
dkxmgrid.DataSource = ds.Tables("贷款行为细目表")
dkxmgrid.DataBind()
Dim myCBuilder As New SqlCommandBuilder(da)
' da.DeleteCommand = myCBuilder.GetDeleteCommand
'da.InsertCommand = myCBuilder.GetInsertCommand
da.UpdateCommand = myCBuilder.GetUpdateCommand
da.Update(ds, "贷款行为细目表")
End Sub
运行后的提示:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 63: Private Sub dkxmgrid_UpdateCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dkxmgrid.UpdateCommand
行 64: Dim rowChange As DataRow = ds.Tables("贷款行为细目表").Rows.Find(a)
行 65: rowChange.BeginEdit()
行 66: rowChange("细目名称") = (CType(e.Item.Cells(3).Controls(0), TextBox)).Text
行 67: rowChange("分值") = (CType(e.Item.Cells(4).Controls(0), TextBox)).Text
源文件: C:\Inetpub\wwwroot\kehuzixin\dkxwxmgl.aspx.vb 行: 65
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
kehuzixin.dkxwxmgl.dkxmgrid_UpdateCommand(Object source, DataGridCommandEventArgs e) in
C:\Inetpub\wwwroot\kehuzixin\dkxwxmgl.aspx.vb:65
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292