0

我在使用我定义的函数填充子网格视图时遇到问题。我不断收到错误“对象引用未设置为对象的实例”。我究竟做错了什么?我是否错误地使用了 FindControl 功能?它似乎没有找到子gridview。

Sub RecordsByZip()
        Dim DBConn As New SqlConnection(Application("DBConn"))
        Dim gv1 As GridView
        gv1 = grdTotal

        Dim gv2 As GridView
        gv2 = DirectCast(gv1.FindControl("grdChild"), GridView)

        Dim ZipCode = lbZip.SelectedItem
        For Each ZipCode In lbZip.Items
            If ZipCode.Selected = True Then

                Dim cmdZip As SqlCommand = New SqlCommand("spPICAInsertTotals2", DBConn)
                cmdZip.CommandType = CommandType.StoredProcedure

                strZip = ZipCode.Text
                strUser = Session("User")

                Dim Zip As New SqlParameter("@Zip", SqlDbType.VarChar)
                Zip.Value = strZip
                cmdZip.Parameters.Add(Zip)

                Dim UserID As New SqlParameter("@UserID", SqlDbType.Int)
                UserID.Value = strUser
                cmdZip.Parameters.Add(UserID)

                DBConn.Open()
                gv1.DataSource = cmdZip.ExecuteReader
                gv1.DataBind()
                gv1.Visible = True
                DBConn.Close()
            End If
        Next
        btnExport.Visible = True
        lblmsg.Visible = False



        ' Dim DBConn = New SqlConnection(Application("DBConn"))
        Dim cmdCounty As SqlCommand = New SqlCommand("spPICAInsertTotals", DBConn)
        cmdCounty.CommandType = CommandType.StoredProcedure
        'Dim gv As GridView = TryCast(e.Row.FindControl("grdChild"), GridView)

        strUser = Session("User")

        Dim UserID2 As New SqlParameter("@UserID", SqlDbType.Int)
        UserID2.Value = strUser
        cmdCounty.Parameters.Add(UserID2)

        DBConn.Open()
        gv2.DataSource = cmdCounty.ExecuteReader
        gv2.DataBind()
        gv2.Visible = True
        DBConn.Close()
        btnExport.Visible = True
        lblmsg.Visible = False
        lblInstructions.Visible = False


    End Sub
4

1 回答 1

0

首先 。. .

作为免责声明,我通常使用中继器控件而不是 gridview 控件。

但这可能会对您有所帮助。. .

我可以告诉你,对于中继器控件,如果你想找到一个嵌套的中继器,那么你必须在它们所属的父中继器的项目中查找它们。本质上,您尝试对上面的代码执行的操作是在实际上可能有多个 grdChild 时找到 grdChild(毕竟它是一个嵌套的 gridview)。我愿意打赌这是您发生对象引用错误的地方。

换句话说,如果你想找到带有 ID 的嵌套中继器nestedRepeater,并且你知道它位于主中继器的第一项(在这种情况下我已分配给myRepeater变量),你可以这样做:

Dim myItem as RepeaterItem = myRepeater.Items(0)
Dim Rep2 as Repeater = myItem.FindControl("nestedRepeater")

使用 SqlDataAdapter 和 DataSet(推荐)

Dim sa As New SqlDataAdapter(cmdCounty) 'Initialize the SqlDataAdapter and assign the SqlCommand object to it.
Dim ds As New DataSet() 'Initialize the DataSet (we will bind this to the gridview)

Try 'The Try/Catch statements help you to handle errors.
    cmdCounty.Connection.Open() 'Open the connection to the database.
    sa.Fill(ds) 'This statement uses the SqlDataAdapter to easily execute the SqlCommand (using the query specified in the SqlCommand object) and . . . 
                '. . .use that data to fill our dataset.
    cmdCounty.Connection.Close() 'These statement close the connection and dispose of the SqlCommand object. Note: You may only need the dispose command.
    cmdCounty.Dispose()

Catch ex As Exception
    'Catch your error here.
    cmdCounty.Connection.Close()
    cmdCounty.Dispose()
End Try

gv2.DataSource = ds 'Set the datasource for your GridView control.
gv2.DataBind() 'Bind the data.

使用 SqlDataReader 和 DataTable

根据您的评论,当您将 gridview 分配给 cmd.ExecuteReader 时,您的代码正在中断。

您将需要使用以下方法访问您的数据:

Dim rdr as SqlDataReader = cmdCounty.ExecuteReader() 'Declare the SqlDataReader and set it to handle your SqlCommand.
Dim dt as New DataTable 'Initialize a new DataTable. This is where we will place the information we read using the SqlDataReader.
'Make sure you add the columns...
dt.Columns.Add("firstColumnName") 'Create a column for each field you will be retrieving data from.
dt.Columns.Add("secondColumnName")
Dim r as DataRow 'Declare the variable r as a DataRow.

'You may want to insert the line "If rdr.HasRows Then" to check if any data was pulled before attempting to read it.
While rdr.Read() 'Loop through each row in the reader.
  r = dt.NewRow() 'Set r to equal a new DataTable in the DataTable we created. Note: This does not actually add the row to the table.
  r("firstColumnName") = rdr("firstColumnName") 'Set the values of each column in the current DataRow to equal their corresponding data read from SQL.
  r("secondColumnName") = rdr("secondColumnName")
  dt.Rows.Add(r) 'Add the DataRow r to the DataTable.
End While 'Loop back until there are no more rows.

gv2.DataSource = dt
gv2.DataBind()

这两个示例都假定您已经创建了 SqlCommand 对象并为其分配了一个有效的 SQL 查询字符串和 Connection 对象。

于 2013-07-22T17:41:02.347 回答