Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


using Combobox displaymemeber and valuemember

Posted on 2009-04-17
Medium Priority
Last Modified: 2013-12-20
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.
Question by:pythonV
LVL 33

Accepted Solution

jppinto earned 2000 total points
ID: 24169209
Use the code below to populate your combobox.

Public Function GetData(ByVal commandText As String) As DataTable
Dim connectionString As String = My.Settings.ConnectionString
Using connection As New SqlConnection(connectionString)
   Using command As New SqlCommand(commandText, connection)
       Using reader As New SqlDataReader = command.ExecuteReader()
            Dim table As New DataTable()
            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


Author Closing Comment

ID: 31571525
thanks, works like a charm.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question