Chris Rasmussen · Photographer · Infrastructure Guy · Code Dabbler · Traveller

MS Access and C# – Syntax error in INSERT INTO statement

Recently I was working on a small application that required writing to a local Microsoft Access database. This was done using Microsoft C# .NET 3.5 (SP1) but every time I clicked the submit button on the relevant form I received an error message saying there was a syntax error in the INSERT INTO statement. Here’s how to fix this error …

For a start, the INSERT statement’s syntax was perfectly fine – I tested it using a new query from within Microsoft Access. The fields in question were called ‘email’ and ‘identifier’ and it turned ‘email’ is a reserved word in Microsoft Access. One of the original SQL statements looked something like this:

INSERT INTO mailinglist (email,identifier) VALUES (‘test@test.com’,'111-111′);

That is one of the statements that caused the error. Because of the Microsoft Access reserved words you must enclose the column names in square brackets, like this:

INSERT INTO mailinglist ([email],[identifier]) VALUES (‘test@test.com’,'111-111′);

Enclosing the column names in square brackets is apparently best practice anyway (as found in many MS SQL Server queries) but nonetheless … problem solved!

banner ad

7 Responses to “MS Access and C# – Syntax error in INSERT INTO statement”

  1. Ron McFlower says:

    Dude u just saved my Life! i was trying to do this four hours man! i searched and searched and studied soo much from ebooks but to no avail. i am doin my project and compatibility issues required that i use ACCESS and everything from CommandBuilders to Insertcommands was giving errors..was finally gonna work on SQL but for this last moment find. thanx Buddy!. All d Best to u in Life!

  2. Rosco says:

    Thanks! Saved what little hair I have left!

  3. chandradev says:

    Thanks for posting this information. I was also getting same error

  4. Zainu says:

    Wow its just funny.Yesterday evening my wife had the same problem and she asked me to help her.I tried but couldn’t find the issue.Now tried it worked.Thanks Buddy.God bless you.
    Regards,
    Zainu

  5. Anu says:

    Thank you so much.
    I also had the same problem and Im breaking my head for 2 days. Your solution resolved my problem. Thank you

  6. johnny says:

    This has saved me tons of work, thanks for posting and ranking for this on google!
    johnny

Leave a Reply

Powered by Wordpress | Designed by Elegant Themes