【问题标题】:ds.tables(0) = 'ds.tables(0)' threw an exception of type 'System.NullReferenceException' vb.netds.tables(0) = 'ds.tables(0)' 抛出类型为 'System.NullReferenceException' vb.net 的异常
【发布时间】:2017-04-13 12:46:58
【问题描述】:

我试图在一个按钮单击事件中从 2 个不同的表中提取数据。我检查了所有内容,似乎没有任何拼写错误或其他任何内容,但一直收到此错误。

下面是我的按钮点击事件代码

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

【问题讨论】:

标签: mysql asp.net vb.net


【解决方案1】:

将所有出现的ds.tables(0) 替换为table。你还没有初始化DataSet ds,但你仍然不需要它,因为你用adapter.Fill(table)填充了DataTable tbl

例如:

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()
    ' ... '

【讨论】:

  • 可以,是的,谢谢:) 但是它仍然不允许我从第二个表(客户)中提取相关文本框的数据,它只是用与第一个表相同的记录填充字段(修复)
  • 非常感谢,现在要弄清楚抛出这个 NullReferenceException 的原因,我应该很好:D
  • @CormacD:我已经向您展示了,您必须添加行 ds = New DataSet()。如果这不能解决问题,您必须提及引发异常的行。
  • 哦,我的错,我真的以为我有这个,很抱歉。谢谢 :) 但是现在当它运行时,尽管我输入了正确的测试数据进行跟踪,但我得到的 msgbox 显示没有找到任何数据
猜你喜欢
  • 2012-12-10
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
相关资源
最近更新 更多