Mysql 表(0)=&x27;ds.表(0)和#x27;引发了类型为';System.NullReferenceException';运动模拟
我试图在一个按钮点击事件中从两个不同的表中提取数据。我已经检查了所有的东西,似乎没有任何打字错误或任何东西,但不断得到这个错误 下面是我的按钮点击事件代码Mysql 表(0)=&x27;ds.表(0)和#x27;引发了类型为';System.NullReferenceException';运动模拟,mysql,asp.net,vb.net,Mysql,Asp.net,Vb.net,我试图在一个按钮点击事件中从两个不同的表中提取数据。我已经检查了所有的东西,似乎没有任何打字错误或任何东西,但不断得到这个错误 下面是我的按钮点击事件代码 Protected Sub btnFindRepair_Click(sender As Object, e As EventArgs) Handles btnFindRepair.Click Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB
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.表(0)
替换为表
。您尚未初始化数据集ds
,但无论如何都不需要它,因为您使用适配器填充数据表tbl
。填充(表)
例如:
If table.Rows.Count > 0 Then
DDLBookedInBy.SelectedItem.Text = table.Rows(0)(2).ToString()
' .... '
如果要填充数据集
,请使用:
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()
' ... '
由于第一个答案,现在修复了@Bugs的可能重复项,但仍然感谢进一步的参考:)为了完整起见,您可以发布与您添加的源代码相关的错误吗?这是可行的,谢谢:)但是它仍然不允许我从第二个表中提取数据(客户)对于相对文本框,它只是用第一个表中相同的记录填充字段(修复),非常感谢,现在来找出引发此NullReferenceException的原因,我应该很好:D@CormacD:我在上面向您展示了,您必须添加行
ds=New DataSet()
。如果这还不能解决问题,你必须提到引发异常的那一行。哦,我的糟糕,我真的以为我在这漫长的一天里遇到了,对不起。谢谢:)但是现在当它运行时,尽管我输入了要跟踪的正确测试数据,但msgbox显示未找到任何数据