using Combobox displaymemeber and valuemember

Hello Experts,

I need to extract two fields from a database and bind one value as text to a combo box and the other as value to be called when the related text is selected.

so the code will look something like this:

while sqldatareader.read = true
           add as displaymember field 0, add as valuemember field1
end while


Thanks in advance, any help is greatly appreciated.
pythonVAsked:
Who is Participating?
 
jppintoCommented:
Use the code below to populate your combobox.

jppinto
Public Function GetData(ByVal commandText As String) As DataTable
 
Dim connectionString As String = My.Settings.ConnectionString
Using connection As New SqlConnection(connectionString)
   connection.Open()
   Using command As New SqlCommand(commandText, connection)
       Using reader As New SqlDataReader = command.ExecuteReader()
            Dim table As New DataTable()
            table.Load(reader)
            Return table
       End Using
   End Using
End Using
 
End Function
 
Sample usage:
Dim dt As DataTable = GetData("Select Name, ID From Table1")
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"

Open in new window

0
 
pythonVAuthor Commented:
thanks, works like a charm.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.