【问题标题】:NullReferenceException - Need to Update FieldNullReferenceException - 需要更新字段
【发布时间】:2013-06-07 21:00:53
【问题描述】:

一直在尝试更新 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

【问题讨论】:

  • 确保rows.DataItemIndex 的值是您数据表中的有效索引。
  • @JoelCoehoorn:实际上,我使用了来自 MSDN 示例的 rows.DataItemIndex

标签: vb.net exception gridview


【解决方案1】:

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

【讨论】:

  • vb.net 允许在没有括号的情况下调用不带参数的方法
  • 谢谢。 .NET 是我的日常,但我已经很久没有接触过 VB 了。编辑。
  • @Renan:在每次调用 ToString 后添加括号,但它不起作用。相同的错误信息。然后我尝试将 .ToString() 更改为 .Text,也没有运气。如果控件没有存储在行中,是否有另一种方法来替换 FindControl?
  • FindControl 方法,当这样使用时,会在调用该方法的控件中搜索用户控件。你确定 GridViewRow 有这些控件吗?我在您的代码中看到您注释掉了 GridView 获取其数据源的部分。如果你没有这样做,gridview 和 DataTable 就可以同步了。
  • @Renan:我注释掉 DataSource 的原因是它也给出了错误 - DataSource 和 DataSourceID 都在“GridView1”上定义。删除一个定义,所以我选择将其注释掉。
猜你喜欢
  • 2021-11-19
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 2022-11-23
  • 1970-01-01
相关资源
最近更新 更多