Solved

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

Posted on 2013-05-29
7
495 Views
Last Modified: 2013-06-04
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?
0
Comment
Question by:VBsqlGuy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 12

Expert Comment

by:duttcom
ID: 39206711
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 -

Visual studio dropdown in Design mode
0
 
LVL 12

Expert Comment

by:duttcom
ID: 39206714
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.
0
 

Author Comment

by:VBsqlGuy
ID: 39207632
I am already using Design view. My ComboBox options look like this:
ComboBox Tasks
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.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Accepted Solution

by:
VBsqlGuy earned 0 total points
ID: 39208120
Figured it out myself.  I set it to Bound, and used these settings:

Combobox Bound setup
Can I accept my own solution, and get 200pts for myself?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39210475
Can I accept my own solution
Yes.

and get 200pts for myself?
No.

I answered my own question. How do I close the question?
0
 

Author Closing Comment

by:VBsqlGuy
ID: 39218385
Figured out my own solution.  Works exactly as I wanted.
0

Featured Post

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question