Solved

Could someone please check out my OleDbCommand problem?

Posted on 2004-09-02
2
273 Views
Last Modified: 2006-11-17
Hello,

I'm having this strange new problem writing to an Access database.  I made a simple test program which executes a simple SQL query, but I'm getting back the exception: "Syntax error in INSERT INTO statement."  And I can't figure out why.  Here's the code...

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
      @"Data source=" + "D:\\DataArchive\\test.mdb";

string sql;
try
{
    conn.Open();
    OleDbCommand dbSelectCommand = new OleDbCommand();
    dbSelectCommand.Connection = conn;
    sql = "INSERT INTO Events (Data) VALUES (22)";
    dbSelectCommand.CommandText = sql;                              
    OleDbDataReader reader = dbSelectCommand.ExecuteReader(); // xxxx            
    reader.Close();
}
catch (Exception ex)
{
}

I made a new Access database ("Test.mdb") and table ("Events") containing a singgle column ("Data") of type "Number".  

The previous code compiles and executes, but once it hits ExecuteReader() it throws the exception: "Syntax error in INSERT INTO statement."  Clearly there's something else going on besies the SQL statement, since the SQL is well formed.  Does anyone have any ideas of what I can try?  I'd be really appreciative since it's got me stopped cold.


Thanks,
Bob
0
Comment
Question by:ba272
2 Comments
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 500 total points
ID: 11968573
I'm not 100% sure, but i don't think you want to user executeReader... you are not reading, but inserting, so there will not be a resultset returned from this... use ExecuteNonQuery() instead, and see if it fixes your problems.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbcommandclassexecutescalartopic.asp


OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   OleDbCommand myCommand = new OleDbCommand(myExecuteQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11970778
Just change ExecuteReader with ExecuteScalar or ExecuteNonQuery
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now