0

Im trying to pull data from 2 different tables in one button click event. I've checked over everything and doesn't seem to be any typo's or anything but keep getting this error.

Below is my code for the button click event

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
CormacD
  • 23
  • 6
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bugs Apr 13 '17 at 12:57
  • @Bugs fixed now due to first answer but thanks anyway for further reference :) – CormacD Apr 13 '17 at 13:05
  • For completeness' sake, can you please post the error related to the source code you added? – PdC Apr 13 '17 at 16:10

1 Answers1

1

Replace all occurences of ds.tables(0) with table. You haven't initialized the DataSet ds but you don't need it anyway because you fill the DataTable tbl with adapter.Fill(table).

For example:

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

If you want to fill the DataSet use:

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()
    ' ... '
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • that works yes thanks :) however it still doesn't let me pull data from the 2nd table (customer) for the relative textboxes it just fills the fields with the same records form the 1st table (repair) – CormacD Apr 13 '17 at 13:04
  • thanks that's much appreciated, now to figure out whats throwing this NullReferenceException and i should be good :D – CormacD Apr 13 '17 at 14:56
  • @CormacD: i have shown you above, you have to add the line `ds = New DataSet()`. If that doesn't fix the issue you have to mention the line where the exception is raised. – Tim Schmelter Apr 13 '17 at 14:57
  • Oh my bad i genuinely thought i had that in, long day sorry. Thanks :) However now when it runs i get the msgbox showing no data was found despite me entering the correct test data to track – CormacD Apr 13 '17 at 15:28