Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Could someone please check out my OleDbCommand problem?

Posted on 2004-09-02
2
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 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