-3

Below is a code and Visual Studio is raising error that there is syntax error near WHERE.

SqlCommand cmd2 = new SqlCommand("insert into courses(t_id_fk) values('" + valu3 + "') where c_id=@dr1 ", con);
cmd2.CommandType = CommandType.Text;
                cmd2.Parameters.Add("@dr1", SqlDbType.Int);
                cmd2.Parameters["@dr1"].Value = d1;
                cmd2.ExecuteNonQuery();
                con.Close();
Niazi
  • 105
  • 11

1 Answers1

4

remove where clause while inserting:

from:

"insert into courses(t_id_fk) values('" + valu3 + "') where c_id=@dr1 "

to:

"insert into courses(t_id_fk) values('" + valu3 + "')"

But if the sample given above is an update then:

 "update courses set t_id_fk = '" + valu3 + "'  where c_id=@dr1"
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30