Why wont my C# sqlCommand work for a simple Update

I’m trying to update a column called Seats_Left in a Table called Events but my simple Update
wont work. Visual Studio keeps pointing to the cmd.ExecuteNonQuery(); statement and saying ‘Incorrect syntax near ‘)’.’ I know the SqlConnection is working fine because it worked for other statements like inserting or SELECT. This should be simple, can you see what I’m doing wrong?:

sqlConnection.Open();

string sql = "UPDATE Events SET Seats_Left=@seats WHERE Title=@title)";

SqlCommand cmd = new SqlCommand(sql, sqlConnection);
cmd.Parameters.AddWithValue("@seats", count);
cmd.Parameters.AddWithValue("@title", title);

cmd.ExecuteNonQuery();
sqlConnection.Close();

I am trying to insert a new integer value into the Seats_Left field for the selected title. For some reason Visual Studio says System.Data.SqlClient.SqlException: ‘Incorrect syntax near ‘)’.’

>Solution :

Hi as mentioned by others, your query ("sql" variable) has a closing parenthesis but not an opening.
Try deleting that bracket or adding the opening and you will see that it works.

sqlConnection.Open();

string sql = "UPDATE Events SET Seats_Left=@seats WHERE Title=@title";

SqlCommand cmd = new SqlCommand(sql, sqlConnection);
cmd.Parameters.AddWithValue("@seats", count);
cmd.Parameters.AddWithValue("@title", title);

cmd.ExecuteNonQuery();
sqlConnection.Close();

Leave a Reply