Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,i need to add the items to combo box from sqlDB.In my form am having 1 combo box(for items) and 1 textbox (for values).i need to load items to both from DB table and the value of textbox should be selected according to value of combo box.

eg:
combobox    textbox
  Items      price
  sss         100
  ddd         140
  fff         220

The textbox value should be selected automatically when combobox value is selected.

C#
private void Form4_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("data source=PC\\SQLEXPRESS;integrated security=true;Initial catalog=MyDB");
            BindData1();
}
 public void BindData1()
        {
            

            con.Open();
            string strCmd = "select Items from tblbill";
            SqlCommand cmd = new SqlCommand(strCmd, con);
            SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cmd.ExecuteNonQuery();
            con.Close();

            comboBox1.DisplayMember = "items";
            comboBox1.ValueMember = "items";
            comboBox1.DataSource = ds.Tables[0];

            comboBox1.Enabled = true;

        }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            con.Open();
           string sel = comboBox1.SelectedItem.ToString();
            
            
           
           SqlCommand cmd = new SqlCommand("select price from tblbill where items=" + comboBox1.SelectedValue, con);
            
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                p = dr[0] as string;
                //department = reader[1] as string;
                
            }
            
            textBox1.Text = p;
            con.Close();
        }


My combobox is filled with items from tblbill but can't able to display price value in textbox according to selection in combobox.
Please help me...
Posted
Updated 22-Nov-13 3:15am
v2
Comments
joshrduncan2012 21-Nov-13 10:27am    
What have you tried to do so far to accomplish this?
Maciej Los 21-Nov-13 11:14am    
Good question! My virtual 5!
joshrduncan2012 21-Nov-13 11:26am    
Thanks!
Nelek 21-Nov-13 13:26pm    
For future comments like this you might find What have you tried?[^] an interesting link :)
joshrduncan2012 21-Nov-13 13:32pm    
Thanks Nelek!

1 solution

You have two distinct problems you are trying to solve: (1) loading content from a database, and (2) updating a text box according to the current selection of the combobox.

Depending on your UI environment, you may have the use of a combobox that can take a collection as its data source. If this is the case then your DB code should return some collection entity (such as a DataTable, or List<mycustomtype> and you can pass that to the combobox. You then tell the combo box what members of the collection type are the display and value members. The display member is what the combobox user sees in the list; the value is what the code can use.

Now, handle the 'selection changed' or similar event. When that event fires, update your text box from the combobox's value (or selected value, or whatever) property.
 
Share this answer
 

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