2017-04-13 52 views
0

我試圖從一個按鈕單擊事件中的2個不同的表中拉數據。我檢查了一切,似乎沒有任何錯字或任何東西,但不斷收到此錯誤。ds.tables(0)='ds.tables(0)'拋出類型'System.NullReferenceException'的異常vb.net

下面是我的按鈕單擊事件

Protected Sub btnFindRepair_Click(sender As Object, e As EventArgs) Handles btnFindRepair.Click 

    Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ITrepair.mdf;Integrated Security=True") 

    Dim command As New SqlCommand("SELECT * from Repair; SELECT * FROM Customer WHERE Tracking_Number = @Tracking_Number", connection) 

    command.Parameters.Add("@Tracking_Number", SqlDbType.Int).Value = txtTrackingNumber.Text 

    Dim adapter As New SqlDataAdapter(command) 
    Dim ds As System.Data.DataSet 
    Dim table As New DataTable() 

    adapter.Fill(table) 


    'Repair Details 
    DDLBookedInBy.SelectedItem.Text = "" 
    DDLDeviceType.SelectedItem.Text = "" 
    txtBookedInDate.Text = "" 
    txtDeviceName.Text = "" 
    DDLAccessories.SelectedItem.Text = "" 
    txtDevicePassword.Text = "" 
    DDLRepairType.Text = "" 
    txtTechnical.Text = "" 
    txtCompletedNotes.Text = "" 
    DDLRepairStatus.Text = "" 

    'Customer Details 

    txtFname.Text = "" 
    txtLname.Text = "" 
    txtContactNum.Text = "" 
    txtAltContactNum.Text = "" 
    txtAddress.Text = "" 


    If table.Rows.Count() > 0 Then 

     ' return only 1 row 
     DDLBookedInBy.SelectedItem.Text = ds.tables(0).Rows(0)(2).ToString() 
     DDLDeviceType.SelectedItem.Text = ds.tables(0).Rows(0)(3).ToString() 
     txtBookedInDate.Text = ds.tables(0).Rows(0)(4).ToString() 
     txtDeviceName.Text = ds.tables(0).Rows(0)(5).ToString() 
     DDLAccessories.SelectedItem.Text = ds.tables(0).Rows(0)(6).ToString() 
     txtDevicePassword.Text = ds.tables(0).Rows(0)(7).ToString() 
     DDLRepairType.Text = ds.tables(0).Rows(0)(8).ToString() 
     txtTechnical.Text = ds.tables(0).Rows(0)(9).ToString() 
     txtCompletedNotes.Text = ds.tables(0).Rows(0)(10).ToString() 

     txtFname.Text = ds.tables(1).Rows(1)(4).ToString() 
     txtLname.Text = table.Rows(1)(5).ToString() 
     txtContactNum.Text = table.Rows(1)(6).ToString() 
     txtAltContactNum.Text = table.Rows(1)(7).ToString() 
     txtAddress.Text = table.Rows(1)(8).ToString() 

    Else 
     MsgBox("NO DATA found") 
    End If 




End Sub 
+1

的【什麼是一個NullReferenceException,如何解決呢?]可能的複製(HTTP:/ /stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bugs

+0

@Bugs現在由於第一次回答而得到修復,不過謝謝你的進一步參考:) – CormacD

+0

爲了完整性'清酒,你可以請張貼與源代碼有關的錯誤喲你補充嗎? – PdC

回答

1

table替換的ds.tables(0)所有出現的代碼。您尚未初始化DataSet ds,但您不需要它,因爲您將DataTable tbl填入adapter.Fill(table)

例如:

If table.Rows.Count > 0 Then 
    DDLBookedInBy.SelectedItem.Text = table.Rows(0)(2).ToString() 
    ' .... ' 

如果你想填補DataSet使用:

Dim ds As System.Data.DataSet 
Dim table As New DataTable() 

ds = New DataSet() 
adapter.Fill(ds) 


If table.Rows.Count > 0 Then 
    DDLBookedInBy.SelectedItem.Text = ds.Tables(0).Rows(0)(2).ToString() 
    ' .... ' 
    txtFname.Text = ds.Tables(1).Rows(1)(4).ToString() 
    ' ... ' 
+0

工作是謝謝:)但它仍然不會讓我從第二個表(客戶)的相對文本框拉數據它只是填充字段與第1表(修復) – CormacD

+0

謝謝非常感謝,現在要弄清楚是什麼拋出了這個NullReferenceException,我應該是好的:D – CormacD

+0

@CormacD:我已經向你展示過了,你必須添加'ds = New DataSet()'行。如果這不能解決問題,則必須提及引發異常的行。 –

相關問題