ashish ashish - 1 year ago 88
ASP.NET (C#) Question

SQL update query in 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


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 Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download