I see many of the answers to similar question where people are saying that in order to get the value of the item loaded in combobox you need to use
combobox1.displayMamer =""
combobox1.valuemember=""
combobox1.datasource=""
But this stuff does not work.....
here is what I have....
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using con As New SqlConnection(sConnection)
con.Open()
Using com As New SqlCommand("Select Code1, Code2 from tblTable6 where fldname ='Things'", con)
Using rdr = com.ExecuteReader
If rdr.HasRows Then
Do While rdr.Read = True
ComboBox1.Items.Add(rdr.GetString(0))
''''missing something here
Loop
con.Close()
End If
End Using
End Using
End Using
End Sub
I'm selecting Code1 and Code2 from Table, i want to be able to display code1 and when selected, I want to be able to have the value of Code2, but with displayMember, and ValueMember, I'm not seeing any result.
EDIT: Here's all my code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using con As New SqlConnection(sConnection)
Using com As New SqlCommand("Select Label, Code from Table.....", con)
con.Open()
Dim dt As New DataTable()
Dim rows = dt.Load(com.ExecuteReader)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Code"
ComboBox1.ValueMember = "Label"
con.Close()
End Using
End Using
End Sub
Dim rows = dt.Load(com.ExecuteReader) --- this line gets underlined
ERROR says: Expression does not produce value
EDIT2:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using con As New SqlConnection(sConnection)
con.Open()
Using com As New SqlCommand("Select Label, Code from tblData where fldname ='M'", con)
Dim dt As New DataTable()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Code"
ComboBox1.ValueMember = "Label"
con.Close()
End Using
End Using
End Sub
Now I get another error saying that: Cannot bind to the new value member. This happens on combobox1.valuemember="Label"
Do While rdr.Read
will just skip the loop if there aren't any rows. – LarsTechdt.Rows.Count
. sorry – Ňɏssa Pøngjǣrdenlarp