2013-07-22 34 views
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 
+1

你在這一行上放了一個斷點嗎?gv2 = DirectCast(gv1.FindControl(「grdChild」),GridView)'看看gv2執行後是什麼? –

+0

在哪一行發生異常? –

回答

0

首先。 。 。

就像一個免責聲明,我通常使用中繼器控件而不是gridview控件。

但這可能會幫助你。 。 。

我可以告訴你,使用中繼器控件,如果你想找到一個嵌套的中繼器,那麼你必須在它們所屬的父中繼器的內部尋找它們。從本質上講,你試圖用上面的代碼來做什麼,當可能實際上有幾個grdChild(畢竟它是一個嵌套的gridview)時,找到grdChild。我願意打賭這是你的對象引用錯誤發生的地方。換句話說,如果你想找到ID爲nestedRepeater的嵌套中繼器,並且你知道它位於你的主中繼器的第一項(在這種情況下,我已經分配了myRepeater變量),你可以這樣做:

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

使用的SqlDataAdapter和數據集(推薦)

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查詢字符串和連接對象將其。

+0

我插入了一個斷點,錯誤發生在這裏.. –

+0

gv2.DataSource = cmdCounty.ExecuteReader 對不起,我忘了輸入代碼。 –

+0

對不起,我花了這麼長時間迴應。我是這個論壇的新手,看不到回覆。 –