473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

INSERT syntax error OleDB

Hi

Can anyone tell me why I get a SQL syntax error with the following code?

string strInsert = "INSERT INTO dateEntry (entry, date) VALUES ('test3',
'17/08/2004')";

OleDbCommand cmd = new OleDbCommand(strInsert, oleDbConnection1);

try
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
tbxOutput.Text = ex.Message;
}
finally
{
oleDbConnection1.Close();
}
Nov 16 '05 #1
11 2582
KavvY <k@u.r> wrote:
Can anyone tell me why I get a SQL syntax error with the following code?

string strInsert = "INSERT INTO dateEntry (entry, date) VALUES ('test3',
'17/08/2004')";

OleDbCommand cmd = new OleDbCommand(strInsert, oleDbConnection1);

try
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
tbxOutput.Text = ex.Message;
}
finally
{
oleDbConnection1.Close();
}


My guess is that the date parameter is in a dodgy format. Rather than
rely on getting it right in your SQL text, use parameters. See
OleDbParameter or OleDbCommand.Parameters for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi,

First of all, what is the error message you are getting?
Can you execute the same query in the query analyser without problem?

the query seems to be ok though.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"KavvY" <k@u.r> wrote in message
news:qv*******************@news-text.cableinet.net...
Hi

Can anyone tell me why I get a SQL syntax error with the following code?

string strInsert = "INSERT INTO dateEntry (entry, date) VALUES ('test3',
'17/08/2004')";

OleDbCommand cmd = new OleDbCommand(strInsert, oleDbConnection1);

try
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
tbxOutput.Text = ex.Message;
}
finally
{
oleDbConnection1.Close();
}

Nov 16 '05 #3
"KavvY" <k@u.r> wrote in message
news:qv*******************@news-text.cableinet.net...
Hi

Can anyone tell me why I get a SQL syntax error with the following code?

string strInsert = "INSERT INTO dateEntry (entry, date) VALUES ('test3',
'17/08/2004')";

OleDbCommand cmd = new OleDbCommand(strInsert, oleDbConnection1);

try
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
tbxOutput.Text = ex.Message;
}
finally
{
oleDbConnection1.Close();
}


KavvY,

Does your exception or message look like the following?

The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.
The statement has been terminated.

This is because the datetime value as a string you entered is in the form of
dd/mm/yyyy. SqlServer can be set up to accept those dates but it is more the
wiser to just use the format of mm/dd/yyyy.

Hope this helps :)

Mythran
Nov 16 '05 #4
If he had query analyzer, he'd be using SqlCommand and SqlConnection.

It's unlikely that he's using SQL Server.
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

First of all, what is the error message you are getting?
Can you execute the same query in the query analyser without problem?

the query seems to be ok though.
cheers,

Nov 16 '05 #5
On Fri, 06 Aug 2004 11:09:29 -0500, Mike Newton confounded his critics by
announcing:
If he had query analyzer, he'd be using SqlCommand and SqlConnection.

It's unlikely that he's using SQL Server.

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

First of all, what is the error message you are getting?
Can you execute the same query in the query analyser without problem?

the query seems to be ok though.

cheers,


The error message is along the lines of "The Sql statement contains a
syntax error.". The exact error escapes me as I'm at home now. but its no
more infomative than that!.

I am accessing an access database using the OLEDB and both fields which I
am trying to write to (date & entry) are strings (or text in Access).

Thanks,
Rich.
Nov 16 '05 #6
On Fri, 6 Aug 2004 08:31:51 -0700, Mythran confounded his critics by
announcing:
KavvY,

Does your exception or message look like the following?

The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.
The statement has been terminated.

This is because the datetime value as a string you entered is in the form of
dd/mm/yyyy. SqlServer can be set up to accept those dates but it is more the
wiser to just use the format of mm/dd/yyyy.

Hope this helps :)

Mythran


Mythran,

Thanks for your reply, the fields I am writing to are both strings (see
other reply for more details). I have to agree though with your suggestion
about updating dates on an SQL server, with another project I found that I
had to write to SQL using US format, and SQL would then proceed to save it
as UK format. Very confusing!

Thanks,
Rich.
Nov 16 '05 #7
It's nice to have critics. ;-)
Kavvy wrote:
On Fri, 06 Aug 2004 11:09:29 -0500, Mike Newton confounded his critics by
announcing:

If he had query analyzer, he'd be using SqlCommand and SqlConnection.

It's unlikely that he's using SQL Server.

Ignacio Machin ( .NET/ C# MVP ) wrote:

Hi,

First of all, what is the error message you are getting?
Can you execute the same query in the query analyser without problem?

the query seems to be ok though.

cheers,

The error message is along the lines of "The Sql statement contains a
syntax error.". The exact error escapes me as I'm at home now. but its no
more infomative than that!.

I am accessing an access database using the OLEDB and both fields which I
am trying to write to (date & entry) are strings (or text in Access).

Thanks,
Rich.

Nov 16 '05 #8
Doesn't access have a wacko version of SQL, or did they fix that?
Wouldn't it be something like...

INSERT INTO [TABLE] -with the literal [ ] being there?

This is how the SQL designer in access works, if you don't select "Use
SQL Server compatible syntax" in the options->tools menu.

Kavvy wrote:
On Fri, 06 Aug 2004 11:09:29 -0500, Mike Newton confounded his critics by
announcing:

If he had query analyzer, he'd be using SqlCommand and SqlConnection.

It's unlikely that he's using SQL Server.

Ignacio Machin ( .NET/ C# MVP ) wrote:

Hi,

First of all, what is the error message you are getting?
Can you execute the same query in the query analyser without problem?

the query seems to be ok though.

cheers,

The error message is along the lines of "The Sql statement contains a
syntax error.". The exact error escapes me as I'm at home now. but its no
more infomative than that!.

I am accessing an access database using the OLEDB and both fields which I
am trying to write to (date & entry) are strings (or text in Access).

Thanks,
Rich.

Nov 16 '05 #9
On Fri, 06 Aug 2004 14:15:27 -0500, Mike Newton confounded his critics by
announcing:
Doesn't access have a wacko version of SQL, or did they fix that?
Wouldn't it be something like...

INSERT INTO [TABLE] -with the literal [ ] being there?

This is how the SQL designer in access works, if you don't select "Use
SQL Server compatible syntax" in the options->tools menu.


Ah yes, good point!

Thanks, I'll check it out :)
Nov 16 '05 #10
This is why it's better to use a SQL Parameter. You can declare it as a
type of date, convert the date on your machine (where you can see the date
time settings), and set it to the parameter.

No more confusion.

SQL Server does care. If you code a date in a string, it needs to be in the
format "mm/dd/yyyy" unless you made some other provisions when setting it
up.

--- Nick

"Kavvy" <r@hfeajhjgg.gt> wrote in message
news:9t*****************************@40tude.net...
On Fri, 6 Aug 2004 08:31:51 -0700, Mythran confounded his critics by
announcing:
KavvY,

Does your exception or message look like the following?

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

This is because the datetime value as a string you entered is in the form of dd/mm/yyyy. SqlServer can be set up to accept those dates but it is more the wiser to just use the format of mm/dd/yyyy.

Hope this helps :)

Mythran


Mythran,

Thanks for your reply, the fields I am writing to are both strings (see
other reply for more details). I have to agree though with your suggestion
about updating dates on an SQL server, with another project I found that I
had to write to SQL using US format, and SQL would then proceed to save it
as UK format. Very confusing!

Thanks,
Rich.

Nov 16 '05 #11
KavvY,
date is a reserved word so you should have it quoted ([date] or `date`)
in your text.

Ron Allen
"KavvY" <k@u.r> wrote in message
news:qv*******************@news-text.cableinet.net...
Hi

Can anyone tell me why I get a SQL syntax error with the following code?

string strInsert = "INSERT INTO dateEntry (entry, date) VALUES ('test3',
'17/08/2004')";

OleDbCommand cmd = new OleDbCommand(strInsert, oleDbConnection1);

try
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
tbxOutput.Text = ex.Message;
}
finally
{
oleDbConnection1.Close();
}

Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Luminal | last post by:
Greetings I'm having some problems on my C# application. I'm using an access database and I'm not able to do select queries with the ' character. My code is this: // some previous code like...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
3
by: Nathan Sokalski | last post by:
When trying to submit data to an Access database using ASP.NET I recieve the following error: System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) +41...
1
by: Joe | last post by:
Hello All, I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message, System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. ...
5
by: mabond | last post by:
Hi VB.NET 2005 Express edition Microsoft Access 2000 (SP-3) Having trouble writing an "insert into" command for a Microsoft table I'm accessing through oledb. I've tried to follow the same...
4
by: Warex | last post by:
Maybe someone can help, my code works untill it gets to update when it tells me Syntax error in INSERT INTO statement. Thought this was automaticaly created according to MS. Im lost, my code is...
6
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
2
by: abedt | last post by:
I got the following error: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where...
1
by: gilesy | last post by:
Hi, I have a ploblem with an insert statement using an access database, I have the same code with a sql database which works but it doesn't seem to work on access. Could someone please help. For...
3
by: gilesy | last post by:
Hi, I have a ploblem with an insert statement using an access database, I have the same code with a sql database which works but it doesn't seem to work on access. Could someone please help. For i =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.