Error while using "SqlCommand"

Posted by Tulip2012 under C# on 2/19/2012 | Points: 10 | Views : 849 | Status : [Member] | Replies : 4
private void button3_Click(object sender, EventArgs e)
{

SqlCommand cmd = new SqlCommand ("Insert into detail values(" + Int32.Parse(textBox1.Text) + ",' " + textBox2.Text + " '," + Int32.Parse(textBox3.Text) + "," + Int32.Parse(textBox4.Text) + "," + Int32.Parse(textBox5.Text) + ")", con1);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted.......");

DataBind();
}

While debugging the above script i am getting the following error----


Error 1 Expected class, delegate, enum, interface, or struct





Responses

Posted by: Raj_Chennai on: 2/19/2012 [Member] Starter | Points: 25

Up
0
Down
hi..
provide the sqlserver database table design script so that i can use it and help you to solve this ....

raj

Tulip2012, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Avdesh on: 2/19/2012 [Member] Starter | Points: 25

Up
0
Down
YOU CAN WRITE IT AS:::
protected void Button3_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into detail values(@a,@b,@c,@d,@f)", con1);
cmd.Parameters.AddWithValue("@a", Convert.ToInt32(TextBox1.Text));
cmd.Parameters.AddWithValue("@b",TextBox2.Text);
cmd.Parameters.AddWithValue("@c", Convert.ToInt32(TextBox3.Text));
cmd.Parameters.AddWithValue("@d", Convert.ToInt32(TextBox4.Text));
cmd.Parameters.AddWithValue("@f", Convert.ToInt32(TextBox5.Text));
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted");
DataBind();
}

Thanks And Regards
AVDESH

Tulip2012, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 2/19/2012 [Member] Silver | Points: 25

Up
0
Down
Hi,
Replace private with protected and try once.

Thanks,
Sanjay

Tulip2012, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 2/21/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
try this code:

SqlCommand cmd = new SqlCommand ("Insert into detail values('" + Int32.Parse(textBox1.Text) + "',' " + textBox2.Text + " ','" + Int32.Parse(textBox3.Text) + "','" + Int32.Parse(textBox4.Text) + "','" + Int32.Parse(textBox5.Text) + "')", con1);


Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Tulip2012, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response