Question

I need to add the items to combo box from SQL db .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.

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

Était-ce utile?

La solution 2

if you want to display the Selected Item price from the combobox.

Try This:

 SqlCommand cmd = new SqlCommand("select price from tblbill where items=@itemname", con);
 cmd.Parameters.AddWithValue("@itemname",ComboBox1.SelectedItem.ToString());

 SqlDataReader dr = cmd.ExecuteReader();
            if(dr.Read())
            {
               textBox1.Text= dr[0].ToString();                  
            }

Difference between SelectedText and Text Property of ComboBox n

SelectedText property from MSDN

Gets or sets the text that is selected in the editable portion of a ComboBox.

while Text property from MSDN

Gets or sets the text associated with this control.

Autres conseils

try like this

SqlCommand cmd = new SqlCommand("select price from tblbill where items=@price", conn);
command.Parameters.AddWithValue("@price", comboBox1.SelectedValue.ToString());
//no need to use reader use 
var value= (int)cmd.ExecuteScalar();//set data type according to your use
textBox1.Text = value.ToString();

Make use of sqlparameter to avoide sql injection and make use of execute scalar as you are trying to get only one price for selected product

This example below is not exactly solves your issue but it:

  • Shows appropriate way of using disposable objects such as SqlConnection and SqlCommand
  • shows that you don't need to load Dataset, DataTable is enough in many cases
  • shows how to bind textbox to data

Using this, you can imagine, how easy would be to load another DataTable with single record and bind it. Or you can manually populate text boxes.

Another option, and this is interesting, is to load a Dataset with two tables - 1 combo lookup and another actual data. Establish relationship between them and bind controls. Then you will not have to go to DB every time you change value in combobox. You will need to refresh data sometimes but everything will be happening on the client.

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()
{
    DataTable dt = null;

    using (SqlConnection con =  new SqlConnection("data source=PC\\SQLEXPRESS;integrated security=true;Initial catalog=MyDB"))
    {
        con.Open();
        string strCmd = "select Items from tblbill";
        using (SqlCommand cmd = new SqlCommand(strCmd, con))
        {
            SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
            dt = new DataTable("TName");
            da.Fill(dt);
        }
    }

    comboBox1.DisplayMember = "field1";
    comboBox1.ValueMember = "field2";
    comboBox1.DataSource = dt;

    textBox1.DataBindings.Add(new Binding("Text", dt, "field3"));

    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top