The following code snippet is for storing values in a table from grid view . And it is showing Unhandled SQLException at the line y.ExecuteNonQuery
the error is Syntax error near ')'
however i cannot determine what's wrong with the code. Help me out.
private void button1_Click(object sender, EventArgs e)
{
string subtable = "";
if (comboBox1.Text == "Physics")
{ subtable = "sub1"; }
else if (comboBox1.Text == "Chemistry")
{ subtable = "sub2"; }
else if (comboBox1.Text == "Maths")
{ subtable = "sub3"; }
else
{ subtable = "sub4"; }
x = new SqlConnection("Data Source= AHSUYA ;initial catalog=master;integrated security=sspi;");
x.Open();
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
y = new SqlCommand("Insert into " + subtable + "values(@a,@b,@c)",x);
y.Parameters.AddWithValue("@a", dateTimePicker1.Value.Date.ToShortDateString());
y.Parameters.AddWithValue("@b", dataGridView1.Rows[i].Cells[0].Value.ToString());
y.Parameters.AddWithValue("@c", dataGridView1.Rows[i].Cells[1].Value.ToString());
y.ExecuteNonQuery();
}
x.Close();
}
`