Solved

Populate ComboBox from SQL Table, based on other ComboBox

Posted on 2013-05-31
10
936 Views
Last Modified: 2013-06-07
I have seen this question all over the general Internet, and on E.E, but have not seen a viable answer.  I am using Visual Studio 2010, building a Windows Form project with Visual Basic.  I have a ComboBox, where the user selects the Site (cboSite).  I want the cboLines ComboBox to contain only those Lines associated with the Site chosen (from the Lines table). The form loads with all ComboBoxes populated, and the user has the chance to change the default entries. The default Site comes from a Global Public variable, chosen at time of login.
I've seen some suggested code involving GetConnectionString, SQLConnection(ConnectionString), SQLDataAdapter, etc.   Everything I have tried in my project get's rejected.

I assume I will put some code in the Form Load event, to populate with the default, then put the same code in the SelectedIndexChanged event, in case the user selects a new Site.

I set strSite = cboSite.selectedvalue, and I setup a SQL string like this;
strSQL = "Select Line from Lines Where Site = '" & strSite & "'"

Other than that, I'm pretty lost.
0
Comment
Question by:VBsqlGuy
  • 5
  • 3
  • 2
10 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39212871
0
 

Author Comment

by:VBsqlGuy
ID: 39213092
Well, that is an interesting article, and is food for thought.  However, it is from 2007, and does not involve SLQ server at all.  It builds a couple of data tables on its own, and uses those.  In Visual Studio 2010, I have things like TableAdapterManager, FillBy, LinesTableAdapter, LinesSitesLinesBindingSource, all feeding from my Production_DataSet.  Since I have a SQL server database, with over 100 tables, I need to know how to handle all these BindingSources and TableAdapters, as they relate to filling a ComboBox as filtered by a previous ComboBox.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39213436
The article is using self made datatable because it is a lot easier for people to reproduce without setting complex environment. Replace my data tables with you'd no matter how they are filled.
0
 

Author Comment

by:VBsqlGuy
ID: 39213572
Most of my difficulty is using Visual Studio's TableAdapter,  BindingSource,  and FillBy tools.  The article in no way mentions these things, and is therefore not very helpful.
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 39214021
If you are not willing to invite using binding source, means you would have to recall the database to filter the combobox again, if this is what you want, then I don't quite get what problem you are facing.

Say: depending on selected "site" your combobox2 should have a list of three, A,B and C, so where you define this? Inside the database?

If so, then easily call another datareader to filter what you should get based on the "selected site" and rebind or repopulate the combobox2. (should by your line table).

For this to work, obviously not considering having other tools available, you must let the combobox1 to be "auto posting", so any changes on this combobox, it reloads.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:VBsqlGuy
ID: 39214490
I'm sorry that I don't understand your English very well.  But, I will use a binding source, I just need to know how to use that properly, in this instance with Visual Studio.

"Easily call another datareader to filter" ?  OK, if that is easy, how is it done?

How do you set a combobox to "auto posting"??
0
 

Accepted Solution

by:
VBsqlGuy earned 0 total points
ID: 39215220
OK, none of this helped me at all.  I figured it out myself....again.  Here's the answer when using Visual Studio 2010:

From the Toolbox\Data - drag a BindingSource to the form (I named it LinesBindingSource)
Set the DataSource for this to your SQL data source (mine is Production_DataSet)
Set the DataMember to the field that you want to show in your ComboBox. (mine is Lines)
Drag a ComboBox onto the form (I named mine cboLine)
Set the Data Source to your new BindingSource you just created, and set the Display and Value members up, as shown here.
cboLine Setup
In code, at form load, and at the reference cbo change event (cboSite_SelectedIndexChanged)
Set a filter on the new BindingSource, that is the SelectedValue of the reference cbo, like this:

LinesBindingSource.Filter = "Site = '" & cboSite.SelectedValue & "'"

Open in new window


This is the only bit of Code involved.  One single, simple line of code.  This works perfectly.

Go me!
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39215781
what is a solution for you is not for everybody else!

BTW, everything you are doing through the UI, my article does something very similar by code!
0
 

Author Closing Comment

by:VBsqlGuy
ID: 39228406
Everything I found in EE and in Google searches in general, did not even get me close to a solution.  My question here was barely glanced at, and the other answers did not lead me in the right direction.  I thought I stated the question very clearly, but the other answers seemed to miss my main points entirely  (English not being first language may be a problem).
I totally figured out the answer on my own, and put what I felt was a complete actual answer to this question in EE, in order to help others who were as frustrated as I with the incomplete answers found to date.
I think EE should setup a Visual Studio expert area specifically, with sub areas for the different versions.
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 39228828
That's not the point. Guiding you are all experts, but you are programming using an end-user's concept, that's why communication breakdown.

Good programmers usually don't use UI components and preset settings there, those are idiot proved objects.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now