Link to home
Start Free TrialLog in
Avatar of VBsqlGuy
VBsqlGuy

asked on

Fill ComboBox from SQL (show one field, select another)

I have my SQL server DataSources all setup.  I drag a ComboBox over to my Windows Form.  I'd think I could just work with the properties under Databindings, like Datasource, and choose what table I want, what field I want to show in the ComboBox, and what field I want saved to the variable, once chosen.  There are properties there like "DisplayMember" and "ValueMember", but I can't get anything to work out.  This is extremely easy to do in Access.  Why is it so hard in Visual Studio 2010??  Is it any easier in VS 2012?

Do I have to write a whole SQL query manually in code?  Do I have to run a loop to the end of the file to assign the values?  Do I run this code in the Form Load Class?  I saw something about Sub Fill_combobox1(Category as String); Can this subroutine be used to fill the box?

My ComboBox is named cboSites, the table is Sites in the Production_DataSet source,  I want all of the entries in the [Site Description] field to show in the ComboBox, and once a user chooses one of the Sites, I want to use the [Site] field for a query later in the form.

How do I make this happen?
Avatar of duttcom
duttcom
Flag of Australia image

Switch to the design mode (bottom left) instead of source mode; when you drag the drop-down control to the page, you will be able to click on the arrow on the right of the new control and set the data source and fields to display and all of the queries will be generated for you from your data source. Like so -

User generated image
I should add that this is a web page shown in the image above, but the principle is the same for Windows forms and is what the Visual refers to in Visual Studio.
Avatar of VBsqlGuy
VBsqlGuy

ASKER

I am already using Design view. My ComboBox options look like this:
User generated image
I've tried both Data bound and Unbound modes.  I can't seem to get either to do anything.  In Unbound, if I [Add Query], it puts a FillBy Toolstrip at the top of my form.  I can preview while designing and get data from my Sites table.  But, when I run the form, the ComboBox is empty.
ASKER CERTIFIED SOLUTION
Avatar of VBsqlGuy
VBsqlGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Anthony Perkins
Can I accept my own solution
Yes.

and get 200pts for myself?
No.

I answered my own question. How do I close the question?
Figured out my own solution.  Works exactly as I wanted.