mysql - ds.tables(0) = 'ds.tables(0)' 抛出类型为 'System.NullReferenceException' vb.net 的异常

标签 mysql asp.net vb.net

我正在尝试通过一键单击事件从 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

最佳答案

将所有出现的 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()
    ' ... '

关于mysql - ds.tables(0) = 'ds.tables(0)' 抛出类型为 'System.NullReferenceException' vb.net 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392753/

相关文章:

mysql - 如何在 mac os 10.13.3 中重置 mysql 密码

PHP, PDO, MySQL & InnoDB,如何用FK更新/删除记录?

每 1 分钟发生一次 MYSQL 事件不起作用

html - Visual Basic 登录页面导致错误(错误 BC30506 Handles 子句需要 WithEvents...)

vb.net - 保存文本框中的数据并保留在下一个 session 中

vb.net - 如何在 VB.Net 中将 'test service' 页面添加到 WCF Web 服务

php - 即使phpMyAdmin连接也无法连接到PHP中的mysql_connect

asp.net - 自定义 OWIN/Katana UserManager 工厂行为

asp.net - 在用户控件(ASCX)中动态设置 Gridview DataNavigateUrlFormatString

c# - 使用 updatepanel 初始工作正常,但一段时间后不起作用