Solved

Getting Text and Values in a COMBOBOX in C#

Posted on 2011-02-18
6
412 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 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