Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Introtxt.Text = Me.ListBox1.SelectedItem.Text
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DbRegister.accdb"
con.ConnectionString = strcon
Dim sql As String = "SELECT CustomerName, CustomerSurname , CustomerType FROM DbCustomer WHERE CustomerName Like '%" & Me.findtxt.Text & "%'"
cmd = New OleDbCommand(sql, con)
Dim adapter As New OleDbDataAdapter(cmd)
ds = New DataSet()
adapter.Fill(ds, "Cuslist")
row_count = ds.Tables("Cuslist").Rows.Count
Dim CusName As String = ""
Dim CusSur As String = ""
Dim CusFull As String
Dim CusType As String = ""
ListBox1.Items.Clear()
For i = 0 To ds.Tables("Cuslist").Rows.Count - 1
CusName = ds.Tables("Cuslist").Rows(i)("CustomerName")
CusSur = ds.Tables("Cuslist").Rows(i)("CustomerSurname")
CusType = ds.Tables("Cuslist").Rows(i)("CustomerType")
CusFull = CusName & " " & CusSur
ListBox1.Items.Add(CusFull)
Next
Label3.Text = CusType <========= ติดตรงนี้ค่ะ ไม่ว่าจะคลิ๊กที่ item ไหน ใน Listbox มันก็ออกเป็น Gold หมดเลยค่ะ ทั้งๆที่มันมีทั้ง Gold กับ Silver
End Sub
มันออกมาแต่ Gold ค่ะ แบบนี้
แล้วพอเราเปลี่ยนโค้ดเป็นแบบนี้ค่ะ
Code (VB.NET)
Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Introtxt.Text = Me.ListBox1.SelectedItem.Text
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DbRegister.accdb"
con.ConnectionString = strcon
Dim sql As String = "SELECT CustomerName, CustomerSurname , CustomerType FROM DbCustomer WHERE CustomerName Like '%" & Me.findtxt.Text & "%'"
cmd = New OleDbCommand(sql, con)
Dim adapter As New OleDbDataAdapter(cmd)
ds = New DataSet()
adapter.Fill(ds, "Cuslist")
row_count = ds.Tables("Cuslist").Rows.Count
Dim CusType As String = ""
For i = 0 To ds.Tables("Cuslist").Rows.Count - 1
CusType = ds.Tables("Cuslist").Rows(i)("CustomerType")
Next
Label3.Text = Me.ListBox1.SelectedItem.Text
End Sub