H. Guy H. Guy - 8 months ago 49
SQL Question

SQL Query incorrect syntax error c#

I'm getting the following error

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near the keyword 'Group'.

When I run the following query

string query = "Update Job Set Name = @Name, Date = @Date, Material = @Material, Instructions = @Instructions, Group = @Group, Time = @Time, Address = @Address Where Id = @Id";

DataRowView drv = (DataRowView)jobGrid.Items.GetItemAt(0);
string name = drv.Row[0].ToString();

int Id = getId(name);

using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
string date = drv.Row[1].ToString();
string material = drv.Row[2].ToString();
string details = drv.Row[3].ToString();
string group = drv.Row[4].ToString();
string time = drv.Row[5].ToString();
string address = drv.Row[6].ToString();

command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@Date", date);
command.Parameters.AddWithValue("@Material", material);
command.Parameters.AddWithValue("@Instructions", details);
command.Parameters.AddWithValue("@Group", group);
command.Parameters.AddWithValue("@Time", time);
command.Parameters.AddWithValue("@Address", address);
command.Parameters.AddWithValue("@Id", Id);


All of those things exist in the database in that order except for Id which comes first. I run a similar update using a different table in the database and it works perfectly. I'm not sure what is wrong with "Group" that makes that error. The value that I insert into Group is a string which is specified by the table ass varchar(50). I am using Visual Studio WPF c#

I can add and delete things from this table perfectly fine, but updating causes this issue

Answer Source

Use brackets for reserved keywords like Date,Group,Time etc

Update Job Set Name = @Name, [Date] = @Date, Material = @Material, Instructions = @Instructions, [Group] = @Group, [Time] = @Time, Address = @Address Where Id = @Id

check Reserved Keywords (Transact-SQL)