Solved

Getting Text and Values in a COMBOBOX in C#

Posted on 2011-02-18
6
414 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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