?
Solved

Could someone please check out my OleDbCommand problem?

Posted on 2004-09-02
2
Medium Priority
?
297 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

765 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