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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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

0
mgmhicks
Asked:
mgmhicks
  • 6
  • 5
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

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.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now