vb.net 在组合框上填充文本框选定的索引已随 sql 数据库更改 [英] vb.net Filling Textbox On combobox Selected Index Changed with sql database
本文介绍了vb.net 在组合框上填充文本框选定的索引已随 sql 数据库更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我编写此代码以在组合框选择索引更改时填充文本框后
after i programming this code for fill text-box when combo-box selected Index changed
我收到此错误未将对象引用设置为对象的实例.",我能做什么??
i got this error 'Object reference not set to an instance of an object.', whats can i do ??
Private Sub participant1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles participant1.SelectedIndexChanged
Try
Dim cmd As SqlCommand = New SqlCommand()
Dim datareader As SqlDataReader = Nothing
If Class1.sqlcon.State = ConnectionState.Open Then
Class1.sqlcon.Close()
End If
Class1.sqlcon.Open()
Dim query As String
query = " select * from tparticipant where namea = '" & participant1.Text & "'"
cmd = New SqlCommand(query, Class1.sqlcon)
While datareader.Read
If datareader IsNot Nothing Then
ID.Text = datareader.GetInt32("ID")
total.Text = datareader.GetInt32("total")
End If
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try`
推荐答案
您需要将 cmd.ExecuteReader()
分配给您的 datareader
.这就是您的 NullReference
的来源.
You need to assign the cmd.ExecuteReader()
to your datareader
. Thats where your NullReference
is coming from.
datareader = cmd.ExecuteReader()
这篇关于vb.net 在组合框上填充文本框选定的索引已随 sql 数据库更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文