Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ‘)’.’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading