I'm making a form on C# that inserts data in a table with a where clause, but it's not working. I think the syntax of my query is the problem but I can't solve it.
This is the one that I tried that's working, but I need an insert query with a where clause.
SqlCommand addEmp = new SqlCommand("INSERT INTO tbl_Expenses " +
"(InvestedMoney,EstimatedServingCount,MealName) " +
"VALUES (@inv, @est, @mname)", conn);
addEmp.Parameters.AddWithValue("@mname", textBox1.Text);
addEmp.Parameters.AddWithValue("@inv", textBox2.Text);
addEmp.Parameters.AddWithValue("@est", textBox3.Text);
conn.Open();
addEmp.ExecuteNonQuery();
conn.Close();
I tried this code below:
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_Meal" +
" ExpensesID, MealName, MealPrice, ImageName, Imageblob)" +
"SELECT ExpensesID, @mname, @mprice, @imname, @img " +
"FROM tbl_Expenses" +
"WHERE MealName = '"+textBox1.Text+"'",conn);
cmd.Parameters.AddWithValue("@mname", textBox1.Text);
cmd.Parameters.AddWithValue("@mprice", textBox4.Text);
cmd.Parameters.AddWithValue("@imname", textBox1.Text);
cmd.Parameters.Add("@img", SqlDbType.Image, photo.Length).Value = photo;
conn.Open();
cmd.ExecuteNonQuery();
But I get this error:
System.Data.SqlClient.SqlException: Incorrect syntax near '='