INSERT syntax error

B

Berend

when I try to write to a database I get a syntax error
I made the code a simple as possible bur also the VS generated code
gives the same error. WHY?


private void button1_Click(object sender, System.EventArgs e)
{
string strSQL;
strSQL = "INSERT INTO Overwerk(datum , begin, eind, omschrijving)
VALUES('10-01-2004', '12:00', '16:00', 'Test')";

string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\\data\\Uren.MDB";


OleDbConnection oleDbConnUren = new OleDbConnection(strDSN);
OleDbCommand myCmd = new OleDbCommand( strSQL, oleDbConnUren );
try
{
oleDbConnUren.Open();
myCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Error:\n{0}", ex.Message);
}
finally
{
oleDbConnUren.Close();
}

}
 
B

Ben Dewey

Berend,

1. Where do you get the error, trying to open the connection or executing
the query?

2. Can you execute the query successfully in a microsoft access query
window?
 
W

William Ryan eMVP

Is this the actual code? If you are getting a syntax error the likely
causes are you are using a Reserved word (from Access in the field name
http://www.knowdotnet.com/articles/reservedwords.html) or you have an
apostrophe somehwere in there.

Put a breakpoint right before the executenonquery and lets see exactly
what's blowing up, the most likely causes are one of the two and since you
mention this isn't thequery used, it's the most likely.

HTH,

Bill


www.devbuzz.com
www.knowdotnet.com
 
B

Berend

William and Ben thanks for your quick respond

Adding brackets to the field names solved the problem
I wil look for the reserved word an change him

Berend
 
J

Joris Janssens

Berend said:
when I try to write to a database I get a syntax error
I made the code a simple as possible bur also the VS generated code
gives the same error. WHY?


private void button1_Click(object sender, System.EventArgs e)
{
string strSQL;
strSQL = "INSERT INTO Overwerk(datum , begin, eind, omschrijving)
VALUES('10-01-2004', '12:00', '16:00', 'Test')";

string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\\data\\Uren.MDB";

doesn't the date have to be surrounded by ## instead of '' when working
with Access?

#10-01-2004# or maybe #10/01/2004#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top