0

我有一个填充数据的 gridview 和一个查看按钮,它提供了用户选择的记录的更多详细信息。我无法弄清楚为什么会出现错误:

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

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

    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
4

2 回答 2

0

最有可能TableRowSection或是TableRowSection.TableHeader/是Nothing

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

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") 

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

于 2013-05-22T17:06:24.803 回答
0

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

这一行的东西:

        ExceptionHist_GV.HeaderRow.TableSection = TableRowSection.TableHeader

尚未使用new关键字实例化。您可以通过检查Exception所抛出的详细信息来确切了解是什么。

它将是ExceptionHist_GVTableRowSection

于 2013-05-22T17:07:24.743 回答