Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button3_Click(object sender, EventArgs e)
        {
            
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\imran\Documents\Visual Studio 2010\Projects\CompleteProject\CompleteProject\Garments.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            con.Open();
            SqlCommand cmd = new SqlCommand(" INSERT INTO OrderStyle (OrderNo,ProductId,SizeId,ColorId,Description) VALUES ('" + comboBox1.SelectedItem.ToString() + "','" + comboBox2.SelectedItem.ToString() + "','" + comboBox3.SelectedItem.ToString() + "','" + comboBox4.SelectedItem.ToString() + "','" + textBox1.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            displayRecords();

        }

please help anyone i can't able to insert data into database using this code using combobox.
Posted
Updated 1-Sep-13 12:29pm
v2
Comments
Boipelo 1-Sep-13 17:47pm    
ProductId, SizeId, ColorId are int? If so, convert the comboboxes; int.Parse(combobox.SelectedValue), what's your error?
[no name] 1-Sep-13 17:48pm    
Okay... so what makes you think that you are unable? Do you get an error of some kind? Why are you using the SQL injection attack waiting to happen code? Why are you not using a parameterized query? Whatever the problem is likely to go away then.
CodeBlack 1-Sep-13 23:49pm    
can you post your code of the displayRecords() method ?
Shambhoo kumar 2-Sep-13 1:04am    
Whhat is your Error message.

1 solution

C#
private void button3_Click(object sender, EventArgs e)
        {
            
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\imran\Documents\Visual Studio 2010\Projects\CompleteProject\CompleteProject\Garments.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            con.Open();
            SqlCommand cmd = new SqlCommand(" INSERT INTO OrderStyle (OrderNo,ProductId,SizeId,ColorId,Description) VALUES (@OrderNo,@ProductId,@SizeId,@ColorId,@Description)", con);

cmd.Parameters.Add(@OrderNo,SqlDbType.Int);
cmd.Parameters.Add(@ProductId,SqlDbType.Int);
cmd.Parameters.Add(@SizeId,SqlDbType.Int);
cmd.Parameters.Add(@ColorId,SqlDbType.Int);
cmd.Parameters.Add(@Description,SqlDbType.NVarChar);
cmd.Parameters["@OrderNo"].Value=int.Parse(comboBox1.SelectedValue.ToString());
cmd.Parameters["@ProductId"].Value=int.Parse(comboBox2.SelectedValue.ToString());
cmd.Parameters["@SizeId"].Value=int.Parse(comboBox3.SelectedValue.ToString());
cmd.Parameters["@ColorId"].Value=int.Parse(comboBox4.SelectedValue.ToString());
cmd.Parameters["@Description"].Value=textBox1.Text;

            cmd.ExecuteNonQuery();
            con.Close();
            displayRecords();
 
        }
 
Share this answer
 
v2

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