对象引用未设置为Object的实例

时间:2013-05-22 17:04:19

标签: vb.net gridview

我有一个填充数据的gridview和一个视图按钮,它提供了用户选择的记录的更多细节。我无法弄清楚为什么我会收到错误:

  

对象引用未设置为Object的实例

我已将其缩小到我的两个消息框之间。第一个消息框出现但它在第二个消息框之前崩溃。任何建议将不胜感激。

    Protected Sub CountAlerts_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles CountAlerts.RowCommand
    If (e.CommandName = "Viewdtails") Then
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim NDC, Unit, Cell, DTTM, prod, Query, _startdt, _enddt As String
        Dim DS As DataSet

        NDC = CountAlerts.DataKeys(index).Values("NDC")
        Cell = CountAlerts.DataKeys(index).Values("Cell")
        Unit = CountAlerts.DataKeys(index).Values("Unit")
        DTTM = CountAlerts.DataKeys(index).Values("TimeDate")
        prod = CountAlerts.DataKeys(index).Values("ProductDesc")

        _startdt = If(StartDate.Text = "", DateAdd(DateInterval.Day, -7, System.DateTime.Now).ToShortDateString, StartDate.Text)
        _enddt = If(EndDate.Text = "", System.DateTime.Now.ToShortDateString, EndDate.Text)
        For Each irow As GridViewRow In CycleCountAlerts.Rows
            If irow.Attributes("class") = "highlight" Then
                irow.Attributes.Remove("class")
            End If
        Next

        CountAlerts.Rows(index).Attributes("class") = "highlight"
        Query = " EXEC [Audits].[dbo].[ExceptionDetailsCombined] '" & NDC & "', '" & Cell & "', '" & Unit & "', '" & DTTM & "', '" & Master.CF_User.Viewing & "' "
        DS = SelectQuery(Query)

        If (DS.Tables.Count > 0) Then
            unitbox.Text = DS.Tables(0).Rows(0)("Unit")
            cellbx.Text = DS.Tables(0).Rows(0)("Cell")
            ndcbox.Text = DS.Tables(0).Rows(0)("NDC")
            namebox.Text = DS.Tables(0).Rows(0)("ProductDesc")
            cycdttmbx.Text = DS.Tables(0).Rows(0)("TimeDate")
            cycusr.Text = DS.Tables(0).Rows(0)("CycUser")
            todisp.Text = DS.Tables(0).Rows(0)("TODISPSIZE")
            topkgbox.Text = DS.Tables(0).Rows(0)("TOPKGSIZE")
            toqtybx.Text = DS.Tables(0).Rows(0)("TOQTY")
            FRQTYbx.Text = DS.Tables(0).Rows(0)("FRQTY")
            TextBox2.Text = DS.Tables(0).Rows(0)("ActualQTY")
            cycvarqbox.Text = DS.Tables(0).Rows(0)("CYCLEVARQTY")
            CycleVarPctbx.Text = DS.Tables(0).Rows(0)("CYCLEVARPCT")
            alertrsnbx.Text = DS.Tables(0).Rows(0)("AlertReason")
            combox.Text = DS.Tables(0).Rows(0)("AcceptComment")
            acusr.Text = DS.Tables(0).Rows(0)("AcceptUser")
            acctime.Text = DS.Tables(0).Rows(0)("AcceptTime")
            accstatbx.Text = DS.Tables(0).Rows(0)("AcceptStatus")
            displbl.Text = DS.Tables(0).Rows(0)("Disposition")
        End If

        Query = " EXEC [CF_Audits].[dbo].[CommentTrackerCombined] '" & Master.CF_User.EmployeeID & "', '" & NDC & "', '" & Cell & "', '" & Unit & "', '" & _startdt & "', '" & _enddt & "', '" & Master.CF_User.Viewing & "' "
        DS = SelectQuery(Query)

        If (DS.Tables.Count > 0) Then
            ExceptionHist_GV.DataSource = DS
            ExceptionHist_GV.DataBind()
            ExceptionHist_GV.UseAccessibleHeader = True
            MsgBox("except gv header") 'Runs up to here. 
            ExceptionHist_GV.HeaderRow.TableSection = TableRowSection.TableHeader
            MsgBox("except gv header 2") ' Does not make it to here.
        End If
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

最有可能TableRowSectionTableRowSection.TableHeaderNothing

在使用它们之前检查它们是否已初始化

If MsgBox("except gv header") 
If Not TableRowSection Is Nothing AndAlso Not TableRowSection.TableHeader Is Nothing Then
    ExceptionHist_GV.HeaderRow.TableSection = TableRowSection.TableHeader
Else
    MsgBox "How did we get here?"
End If
MsgBox("except gv header 2") 

如果您看到“我们是如何到达此处”的消息,表明您尚未初始化对象

答案 1 :(得分:0)

在面向对象的语言中,您需要先实例化一个对象,然后才能使用它。

这一行:

        ExceptionHist_GV.HeaderRow.TableSection = TableRowSection.TableHeader

尚未使用new关键字进行实例化。您可以通过查看抛出的Exception的详细信息找到确切的内容。

它可以是ExceptionHist_GVTableRowSection