Exception on inserting into Access 2010 in C Sharp

Posted by slao.it on Stack Overflow See other posts from Stack Overflow or by slao.it
Published on 2010-12-22T17:29:10Z Indexed on 2010/12/26 8:54 UTC
Read the original article Hit count: 311

Filed under:
|
|

Hello,
I am getting this exception when inserting into a Access 2010 database.

Ex: System.Data.OleDb.OleDbException (0x80040E14): Syntax error in string in query expression ''CityName ?'. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at ReadingData.Program.Main(String[] args) in C:\Users\user\documents\visual studio 2010\Projects\ReadingData\ReadingData\Program.cs:line 238

INSERT INTO CranbrookMain (ID,BlockNo,Plot,SubPlot,Code,Type,LastName,FirstName,ServiceHome,ServiceAddress,ServiceCity,Notes) VALUES ('1','Y','37','DS','C2','O','SMITH','John','Service Inc.','520B SLATER ROAD N.W.','CityName','CityName ? ')

                    insertSQL = "INSERT INTO CranbrookMain (ID,BlockNo,Plot,SubPlot,Code,Type,LastName," +
                        "FirstName,ServiceHome,ServiceAddress,ServiceCity,Notes) VALUES (" +
                         "'"+id+ "','" + blockNo + "','" + plot + "','" + subPlot + "','" + code + "','" + type + "','" + lastname + "','" + firstname + "','" + serviceHome + "','" + serviceAddress + "','" + serviceCity + "','" + notes +"')";
                    Console.WriteLine();
                    OleDbCommand cmd = new OleDbCommand(insertSQL, con); // creating query command
                    cmd.ExecuteNonQuery();

The error occurs in cmd.ExecuteNonQuery() function call.

The above SQL INSERT statement works fine if I directly execute in the Access 2010 file.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ms-access