Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.

What I need is a combo box to show all the values of a column from my SQL database. This part is working. What i need is the selected value not to be the name but the ID of the item.

C#
string conString = @"CONNECTION STRING HERE; Password = PWD";
            SqlConnection conn = new SqlConnection(conString);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from Devices", conn);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);
                
                comboBox1.DisplayMember = "Address";
                comboBox1.ValueMember = "ID";
                comboBox1.DataSource = ds.Tables[0];
                label7.Text = comboBox1.ValueMember;
            }
            catch (Exception ex)
            {
                //Exception Message
            }
        }


The address are shown in the combo, but it appears the value is still the text rather then the ID.

What have i done wrong?

What I have tried:

C#
comboBox1.DisplayMember = "Address";
                comboBox1.ValueMember = "ID";
Posted
Updated 18-Dec-18 20:26pm
v2

Instead of fetching the ValueMember, try to use ListControl.SelectedValue Property (System.Windows.Forms) | Microsoft Docs[^]

For example
label7.Text = comboBox1.SelectedValue.ToString();

Remember to test if a selection has been made by investigating for example ComboBox.SelectedIndex Property (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 14093832 19-Dec-18 3:00am    
Thanks for the replies. Its not the Text value I need its the ID value.

In the database there are two <columns ID and Address. I need the Addresses to be the value shown in the comboboxes. However the the selected value to be the match ID value of the Address in the same row of SQL.
Member 14093832 19-Dec-18 3:05am    
Sorry that did work! Thanks!
Wendelius 19-Dec-18 11:03am    
You're welcome!
Member 14093832 21-Dec-18 4:42am    
Hi, Another question if you dont mind. This works when my form first loads up and the label changes to the ID. However when i change the selection in the combobox the value doesn't change. any suggestions?
Member 14093832 21-Dec-18 4:56am    
Infact if i use the same method on a submit button the value auto changes back to the value the combobox was on load
Just write down like
label7.Text = comboBox1.Text;
 
Share this answer
 
Comments
Member 14093832 19-Dec-18 3:00am    
Thanks for the replies. Its not the Text value I need its the ID value.

In the database there are two <columns ID and Address. I need the Addresses to be the value shown in the comboboxes. However the the selected value to be the match ID value of the Address in the same row of SQL.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900