ashish ashish - 13 days ago 7
ASP.NET (C#) Question

SQL update query in asp.net c# (syntax error)

I am getting error in simple update query for dynamically created table,just can't understand what the error here is..

c# code:

cmd2 = new SqlCommand("UPDATE " + tname + "SET ans=@ans, sans=@sans, chk=@chk WHERE qno=@qno", con99);
cmd2.Parameters.AddWithValue("@ans", ans);
cmd2.Parameters.AddWithValue("@sans", sans);
cmd2.Parameters.AddWithValue("@chk", chk);
cmd2.Parameters.AddWithValue("@qno", qno);
cmd2.ExecuteNonQuery(); //error showing here in vstudio


Error:


An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.dll but was not handled in user code Additional
information: Incorrect syntax near 'ans'

Answer

You are missing a space after the table name and you have an unnecessary closing parentheses :

"UPDATE " + tname + " SET ans=@ans, sans=@sans, chk=@chk WHERE qno=@qno"