Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is my code to load combobox with product name:

C#
public void LoadProduct()
       {
           strconn.Open();

           SqlDataAdapter da = new SqlDataAdapter("Select productID,productName from AS_Product", strconn);
           DataSet ds = new DataSet();
           da.Fill(ds);
           cmbproname.DataSource = ds.Tables[0];
           cmbproname.ValueMember = "productID";
           cmbproname.DisplayMember = "productName";
           strconn.Close();
       }

here is my code to populate textbox:
C#
private void cmbproname_SelectedIndexChanged(object sender, EventArgs e)
     {
          string cm = "SELECT * FROM AS_Product WHERE productName='" + cmbproname.Text + "'";
         SqlDataAdapter da = new SqlDataAdapter(cm, strconn);
         SqlCommandBuilder cmd = new SqlCommandBuilder(da);
         DataSet ds = new DataSet();
         da.Fill(ds);
         if (cmbproname.SelectedIndex >=0)
         {
             txtmrp.Text = ds.Tables[0].Rows[0]["productMRP"].ToString();
         }
     }
Posted
Updated 21-Jan-15 23:43pm
v2
Comments
CHill60 22-Jan-15 5:42am    
And what is the error?
nagendrathecoder 22-Jan-15 6:04am    
You forgot to mention the error.
Richard Deeming 22-Jan-15 7:06am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

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