Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting Text and Values in a COMBOBOX in C#

Posted on 2011-02-18
6
Medium Priority
?
418 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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