tobey1
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?
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();
Can't u pass the selectedvalue like this? string selVal = cbo.SelectedValue;
ASKER
The result is NULL when my next SQL statement has
"WHERE State_ID = '" + cb_State.SelectedValue + "';"
"WHERE State_ID = '" + cb_State.SelectedValue + "';"
R u sure a value is getting passed? in cb_State.SelectedValue
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Figured it out myself with trial an error