Solved

Getting Text and Values in a COMBOBOX in C#

Posted on 2011-02-18
6
409 Views
Last Modified: 2012-05-11
I am trying to create a COMBOBOX from SQL, and I can populate the box with the text from one column, but I need to also keep the values associated with that data element to feed a second SQL select in another COMBOBOX.

Example:  When you select a state (Maine) I want to pass the FIPS code (ID from the DB) to the next SQL statement to run another SQL query.  How can I do this?
 
sqlConn.Open();
            SqlDataAdapter sqlFIPSAdapter = new SqlDataAdapter("SELECT DISTINCT State_FIPS_ID, State_Name " +
                                                               "FROM FIPS_Table " +
                                                               "ORDER BY State_Name;", sqlConn);
            DataTable dtFIPS = new DataTable();
            sqlFIPSAdapter.Fill(dtFIPS);
            cb_State.DisplayMember = "State_Name";
            cb_State.ValueMember = "State_FIPS_ID";
            foreach (DataRow dr in dtFIPS.Rows)
            {
                cb_State.Items.Add(dr[1]);
            }
            this.cb_State.Text = "<Select a State>";
            sqlConn.Close();

Open in new window

0
Comment
Question by:tobey1
  • 4
  • 2
6 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 34928094
Can't u pass the selectedvalue like this? string selVal = cbo.SelectedValue;
0
 
LVL 5

Author Comment

by:tobey1
ID: 34928203
The result is NULL when my next SQL statement has
"WHERE State_ID = '" + cb_State.SelectedValue + "';"
0
 
LVL 20

Expert Comment

by:informaniac
ID: 34929033
R u sure a value is getting passed? in cb_State.SelectedValue
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Author Comment

by:tobey1
ID: 34929061
I added this line as a test in my ..SelectedIndexChanged

MessageBox.Show("change: " + cb_State.Name + " index: " + cb_State.SelectedIndex + " item: " + cb_State.SelectedItem + " value: " + cb_State.SelectedValue);

and it shows:

change: cb_State  index: 1 item: Alabama value: null

It is passing everything except for Selected Value.
0
 
LVL 5

Accepted Solution

by:
tobey1 earned 0 total points
ID: 34929186
I was able to solve it with this code:

            sqlConn.Open();
            SqlDataAdapter sqlFIPSAdapter = new SqlDataAdapter("SELECT DISTINCT State_FIPS_ID, State_Name " +
                                                               "FROM FIPS_Table " +
                                                               "ORDER BY State_Name;", sqlConn);
            DataTable dtFIPS = new DataTable();
            sqlFIPSAdapter.Fill(dtFIPS);
            sqlConn.Close();
            cb_State.SelectedIndex = -1;
            cb_State.DisplayMember = "State_Name";
            cb_State.ValueMember = "State_FIPS_ID";
            cb_State.DataSource = dtFIPS;
            this.cb_State.Text = "<Select a State>";
0
 
LVL 5

Author Closing Comment

by:tobey1
ID: 34959191
Figured it out myself with trial an error
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

12 Experts available now in Live!

Get 1:1 Help Now