datasets and combo boxes

Have to admit vb.net has me feeling a little dumb.  Been using vb6 for some time but cant get the concepts down on some things.  Anyway I filled a dataset with 2 tables.  I need to go through each table and fill a combox box with a field in the tables.  Each table goes to a different combo box.  Thanks in advance

mgmhicksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
VB .Net made this easy!

Add dropdownlists to your form and bind them like this:

ddl1.datasource = ds.tables(0)
ddl1.databind
0
mgmhicksAuthor Commented:
I dont have dropdownlist in my toolbar .  
0
Jeff CertainCommented:
Use combobox. Sorry, they're called dropdownlists in ASP .Net
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

mgmhicksAuthor Commented:
databind is not a function of vb.net for windows.  Using windows not ASP.  Somehow I think I have to go through the dataset and I am not sure how that works

0
Jeff CertainCommented:
So... what happens if you set the datasource property to the table from the dataset? This should still work. You'll also need to set DisplayMember and ValueMember to the appropriate fields.

combobox1.datasource = dt.tables(0)
combobox1.displaymember ="ColumnNameToDisplay"
combobox1.valuemember = "ColumnNameToUseAsValue"
0
karthikeyanTPCommented:

Hai mgmhicks,


Let us say you have US states combo box and you have table called dataTable1 that has two fields "stateName" and "stateID". If you want to display the name of the states in the combo box and you want to update your Database with stateID, this is how you will bind the combo box.

The following is the dynamic databinding, i.e. binding at run time

 ' Dynamic data binding the combo box "cboBoxStates"
With cboBoxStates
                .DataSource = dataTable1                   'If you are using dataset, get the corressponding data table from it and assign it here
                .DisplayMember = "stateName"
                .ValueMember = "stateID"
End With

Another way would be binding the combo box at design time. if you click the properties of the combo box, you will see the properties DataSource, DisplayMember and ValueMemer in VS .NET studio

Cheers
0
mgmhicksAuthor Commented:
ok so if I want the username fields data displayed in the combox box I have

combobox1.displaymember ="Username"
combobox1.valuemember = "Username"

is this correct

0
Jeff CertainCommented:
If you want to return a user ID when the user clicks, you can do this

combobox1.displaymember ="Username"
combobox1.valuemember = "UserID"

This will display the user name, but return the ID as the selected value.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mgmhicksAuthor Commented:
I have a dataset with 2 tables.  Each table has to go to a different combo box.  
0
Jeff CertainCommented:
combobox1.datasource = dt.tables(0)
combobox1.displaymember ="ColumnNameToDisplay"
combobox1.valuemember = "ColumnNameToUseAsValue"
combobox2.datasource = dt.tables(1)
combobox2.displaymember ="OtherColumnNameToDisplay"
combobox2.valuemember = "OtherColumnNameToUseAsValue"
0
mgmhicksAuthor Commented:
Here is the code I have.  the .valuemember line errors out on me.

 myDA = New SqlDataAdapter()
        myDS = New DataSet()
        mySQL = "SELECT * FROM time_employees "

        MyCommand = New SqlCommand(mySQL, MyConnection)
        MyConnection.Open()
        myDA.SelectCommand = MyCommand
        myDA.Fill(myDS, "Employees")

        mySQL = "SELECT * FROM time_payperiod"
        MyCommand = New SqlCommand(mySQL, MyConnection)
        myDA.SelectCommand = MyCommand
        myDA.Fill(myDS, "PayPeriods")

        MyConnection.Close()

        If myDS.Tables(0).Rows.Count = 0 Then
            MsgBox("Problem getting Employees.. Contact Support")
            Exit Function


        Else

            cboEmployees.DataSource = myDS.Tables(0)
            cboEmployees.DisplayMember = "UserName"
            cboEmployees.ValueMember = "Username"

        End If

If there is a better way to set this up please let me know.

thanks
0
Jeff CertainCommented:
don't set .valuemember... and just use the SelectedItem property to ge tthe username out. You're not using .valuemember anyhow.

It may be that .ValueMember is only used when actually binding to the data source? Sorry for any confusion.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.