Solved

Could someone please check out my OleDbCommand problem?

Posted on 2004-09-02
2
275 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
install app on other machine 13 32
Iterate two lists and remove inactive 11 49
DateTimepicker 4 33
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 31
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

12 Experts available now in Live!

Get 1:1 Help Now