0

定義した関数を使用して子グリッドビューを設定する際に問題があります。「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というエラーが表示され続けます。私は何を間違っていますか?FindControl 関数の使い方が間違っていますか? 子グリッドビューが見つからないようです。

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

初めに 。. .

免責事項として、私は通常、グリッドビュー コントロールの代わりにリピーター コントロールを使用します。

しかし、これはあなたを助けるかもしれません。. .

リピーター コントロールでは、ネストされたリピーターを見つけたい場合は、それらが属する親リピーターのアイテム内でそれらを探す必要があると言えます。基本的に、上記のコードでやろうとしていることは、実際には複数の grdChild が存在する可能性があるときに grdChild を見つけることです (結局のところ、ネストされたグリッドビューです)。これは、オブジェクト参照エラーが発生している場所であると確信しています。

つまり、 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 の使用

あなたのコメントによると、グリッドビューを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 に答える