spike.y
spike.y

Reputation: 399

Incorrect syntax near the keyword 'Order' - Order is column name

I keep getting the following exception:

Incorrect syntax near the keyword 'Order'.

I have a TABLE with columns: Name, Credits, Extension, Order. Order is of type bigint. Now when I go to INSERT a single record into this table, it gives me the above exception.

I have put it in a try/catch block and caught the exception, I have set breakpoints and it does not reveal anything other than the above message.

Can anybody please help shed some light on this? I'm sitting here, scratching my head wondering what the heck is going on and I just can't figure it out. I don't see where I've gone wrong.

        try
        {
            // Insert into database
            sqlconnection = new SqlConnection(@"Data Source=sblah blah blah... intentionally  removed;");

            sqlconnection.Open();

            using (var command = new SqlCommand("Insert Into Images(Name, Credits, Extension, Order) VALUES (@Name, @Credits, @Extension, @Order)", sqlconnection))
            {
                command.CommandTimeout = 240;

                command.Parameters.AddWithValue("Name", workingPicture.Properties.Filename);
                command.Parameters.AddWithValue("Credits", workingPicture.Properties.Credits);
                command.Parameters.AddWithValue("Extension", workingPicture.Properties.Extension);
                command.Parameters.AddWithValue("Order", workingPicture.Properties.Order);

                command.ExecuteNonQuery();

                doneUpdatingDB = true;
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
            doneUpdatingDB = false;
        }

The value of Order is 0.

Upvotes: 1

Views: 1626

Answers (2)

Szymon
Szymon

Reputation: 43023

ORDER is a reserved keyword in SQL Server. You have to enclose is in square brackets if you want to use it as a column name:

"Insert Into Images(Name, Credits, Extension, [Order]) VALUES (@Name, @Credits, @Extension, @Order)"

It is best to avoid using reserved keywords to name your objects (tables, columns, stored procs, etc.).

Upvotes: 8

Ishtiaq
Ishtiaq

Reputation: 1058

Order is the keyword in the SQL, Change the name of the field in the DB or make [] around the order keyword like "[order]"

Upvotes: 3

Related Questions