I don't use combo boxes very much, so I basically forgot how to use them. Im connecting to an SQL database and I just want to populate a combo box with a users first name. Heres what I have so far:

DBATimeOff.SelectCommand.CommandText = "SELECT FName FROM tblEmployee"
DBATimeOff.Fill(DataTimeOff)

CmbEmployee.DisplayMember = "FName"
CmbEmployee.ValueMember = "EmployeeID"


Am I even close?

Thanks in advance

Ok once you have a connection set up, the easiest way to go is using a dataset you can basically do this. i did not test this.

Dim statement As String = "SELECT EmployeeId, FName FROM tblEmployee"
Dim adapt As New SqlDataAdapter(statement,connection)
Dim ds As New DataSet()
connection.Open()
adapt.Fill(ds)
connection.Close()
CmbEmployee.DataSource = ds
CmbEmployee.DisplayMember = "FName"
CmbEmployee.ValueMember = "EmployeeID"
CmbEmployee.DataBind()

regards.

I could not get that code to work. Heres my full code if it helps:

OdbcConnection1.Open()


DBATimeOff.SelectCommand.CommandText = "SELECT FName FROM tblEmployee"

DBATimeOff.Fill(DataTimeOff)

CmbEmployee.DataSource = DataTimeOff

CmbEmployee.DisplayMember = "FName"

Wrap source code with BB code tag - See # icon at toolbar.

Post your complete code and error description.

OdbcConnection1.Open()


        DBATimeOff.SelectCommand.CommandText = "SELECT FName FROM tblEmployee"






        DBATimeOff.Fill(DataTimeOff)



        CmbEmployee.DataSource = DataTimeOff

        CmbEmployee.DisplayMember = "FName"

I dont get any errors, but the combo box is not populating. Instead, the combo box is displaying this:
System.Data.DataViewManagerListItemTypeDescriptor

I think something weird is happening with the dataset.

I said "post your complete code".

If DataTimeOff is an instance of DataTable then use following code:

CmbEmployee.DataSource = DataTimeOff
       CmbEmployee.DisplayMember = DataTimeOff.Columns(0).ColumnName

and If DataTimeOff is DataSet instance then use following code:

CmbEmployee.DataSource = DataTimeOff.Tables(0)
       CmbEmployee.DisplayMember = DataTimeOff.Tables(0).Columns(0).ColumnName
commented: Thanks! +1

That did it! thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.