Thread: Question Populating combobox
View Single Post
  #2 (permalink)  
Old 10-09-2008, 11:02 PM
rajhansh rajhansh is offline
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Oct 2008
Age: 25
Posts: 8
Reputation: 0
rajhansh is on a distinguished programming path ahead
Thumbs up

You can simply bind the control to a dataset.

See the code below:

'these are the global variables
Code:
    Private objConnection As OleDbConnection
    Private objCommand As OleDbCommand
    Private objDataAdapter As OleDbDataAdapter
    Private objDataTable As DataTable

'you can use below code in any event (like form load)
Code:
        'Initialize the Connection object
        objConnection = New OleDbConnection(strConnectionString)

        'Initialize the Command object
        objCommand = New OleDbCommand("SELECT ID, FirstName " & _
            "FROM Employee", objConnection)

        'Initialize the DataAdapter object and set the SelectCommand property
        objDataAdapter = New OleDbDataAdapter
        objDataAdapter.SelectCommand = objCommand

        'Initialize the DataTable object
        objDataTable = New DataTable

        'Populate the DataTable
        objDataAdapter.Fill(objDataTable)

        'Bind the DataTable to the ComboBox
        ComboBox1.DataSource = objDataTable
        ComboBox1.DisplayMember = "FirstName"
        ComboBox1.ValueMember = "ID"

        'Clean up
        objDataAdapter.Dispose()
        objDataAdapter = Nothing
        objCommand.Dispose()
        objCommand = Nothing
        objConnection.Dispose()
        objConnection = Nothing
    End Sub
End Class

----------------
Let me know if you need any more help.
Reply With Quote