using Combobox displaymemeber and valuemember

Posted on 2009-04-17
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 = 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

    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

    thanks, works like a charm.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    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…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now