Link to home
Start Free TrialLog in
Avatar of tobey1
tobey1Flag for United States of America

asked on

Getting Text and Values in a COMBOBOX in C#

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

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Can't u pass the selectedvalue like this? string selVal = cbo.SelectedValue;
Avatar of tobey1

ASKER

The result is NULL when my next SQL statement has
"WHERE State_ID = '" + cb_State.SelectedValue + "';"
R u sure a value is getting passed? in cb_State.SelectedValue
Avatar of tobey1

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of tobey1
tobey1
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tobey1

ASKER

Figured it out myself with trial an error