我正在尝试从组合框填充文本框,但只能使用我的数据库中的列名填充它们,如下所示。
[错误示例]:
https://i.imgur.com/jyeUrKM.jpg
这是我的代码。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'DB PATH
dbSource = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=C:\USERS\MICHAEL\DOCUMENTS\GODFREYHOLMES\GHAPP2018\GHAPP2018\BIN\DEBUG\GH2018.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
'Customer ComboBox
Try
sql = "SELECT FirstName,LastName FROM Customer"
cmd.CommandText = sql
cmd.Connection = con
con.ConnectionString = dbSource
con.Open()
rdr = cmd.ExecuteReader
While rdr.Read()
ComboCustomer.Items.Add(rdr(name:="FirstName").ToString)
End While
Catch ex As Exception
MessageBox.Show("error...", "Read Error Recovery", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
con.Close()
End Try
End Sub
Private Sub ComboCustomer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboCustomer.SelectedIndexChanged
If ComboCustomer.SelectedIndex <> -1 Then
'Populate Customer Panel textboxes
Try
sql = "SELECT * FROM Customer WHERE FirstName = FirstName"
'Run time parameters to avoid SQL injections
cmd.Parameters.Add("FirstName", SqlDbType.VarChar).Value = ComboCustomer.SelectedItem.ToString
cmd.CommandText = sql
cmd.Connection = con
con.ConnectionString = dbSource
con.Open()
rdr = cmd.ExecuteReader
While rdr.Read
TxtboxCustomerID.Text = ("CustomerID").ToString
TxtBoxFirstName.Text = ("FirstName").ToString
TxtBoxLastName.Text = ("LastName").ToString
TxtBoxAddress.Text = ("Address").ToString
TxtBoxZip.Text = ("Zip").ToString
TxtBoxTele.Text = ("Telephone").ToString
TxtBoxAccType.Text = ("AccType").ToString
End While
Catch ex As Exception
MessageBox.Show("error...", "Read Error Recovery", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
cmd.Parameters.Clear()
con.Close()
End Try
End If