NullReferenceException - 需要更新字段

时间:2013-06-07 21:00:54

标签: vb.net exception gridview

尝试更新GridView中的字段但没有成功尝试。这是我得到的错误对象引用没有设置为对象的实例。它不断突破dt.Rows(row.DataItemIndex)("TicketID") = (CType(row.FindControl("TicketID"), TextBox)).ToString作为初学者,我不知道如何解决这个问题。非常感谢任何帮助。

代码隐藏:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim dt As New DataTable("historyList")

    dt.Columns.Add("TicketID", GetType(Integer))
    dt.Columns.Add("DateCreated", GetType(DateTime))
    dt.Columns.Add("FullName", GetType(String))
    dt.Columns.Add("TicketType", GetType(String))
    dt.Columns.Add("Subject", GetType(String))
    dt.Columns.Add("Message", GetType(String))
    dt.Columns.Add("Status", GetType(String))

    For i = 0 To 6
        Dim tableRow = dt.NewRow()
        tableRow("TicketID") = i
        tableRow("DateCreated") = Now()
        tableRow("FullName") = i.ToString()
        tableRow("TicketType") = i.ToString()
        tableRow("Subject") = i.ToString()
        tableRow("Message") = i.ToString()
        tableRow("Status") = i.ToString()
        dt.Rows.Add(tableRow)
    Next

    Session("dt") = dt

    BindData()

End Sub

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    BindData()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
    GridView1.EditIndex = 1
    BindData()
End Sub

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Dim dt = CType(Session("dt"), DataTable)

    'if your current DataSource be in Session
    Dim row As GridViewRow = GridView1.Rows(e.RowIndex)

    dt.Rows(row.DataItemIndex)("TicketID") = (CType(row.FindControl("TicketID"), TextBox)).ToString
    dt.Rows(row.DataItemIndex)("DateCreated") = (CType(row.FindControl("DateCreated"), TextBox)).ToString
    dt.Rows(row.DataItemIndex)("FullName") = (CType(row.FindControl("FullName"), TextBox)).ToString
    dt.Rows(row.DataItemIndex)("TicketType") = (CType(row.FindControl("TicketType"), TextBox)).ToString
    dt.Rows(row.DataItemIndex)("Subject") = (CType(row.FindControl("Subject"), TextBox)).ToString
    dt.Rows(row.DataItemIndex)("Message") = (CType(row.FindControl("Message"), TextBox)).ToString
    dt.Rows(row.DataItemIndex)("Status") = (CType(row.FindControl("Status"), TextBox)).ToString

    Session("dt") = dt
    GridView1.EditIndex = 1
    BindData()

End Sub

Private Sub BindData()

    'GridView1.DataSource = Session("dt")
    GridView1.DataBind()

End Sub

1 个答案:

答案 0 :(得分:0)

这可能是因为FindControl方法返回空引用(如果我没记错的话,我在谈论VB中的nothing值)。这意味着控件没有存储在行中,所以请检查。