Going slightly mad - Insert sql not working from code but does in Access?

J

JamesB

I am trying to insert a row to an Access database:

INSERT INTO tblTest (Val1, Val2, Val3, Val4, Val5) VALUES ('blah', 0, '',
'', 'foo');

If I run this in my code:

String insStr = "INSERT INTO INSERT INTO tblTest (Val1, Val2, Val3, Val4,
Val5) VALUES ('blah', 0, '', '', 'foo');
OleDbCommand insCmd = new OleDbCommand(insStr, DBConn);
try
{
insCmd.ExecuteScalar();
}

I get an error "Syntax error in INSERT statement". Yet if I create a new
query in the Access db and paste the insert statement in there, it works
fine! All fields are text apart from Val2, which is a Yes/No field.

Anyone spot what I'm doing wrong here?
 
S

sloan

A.

You want to run
..ExecuteNonQuery

and not the scalar.

2. You need to provide the datatypes of Va1-Val5.
 
S

sloan

OOPS.

You gave us the data types. Sorry.




sloan said:
A.

You want to run
.ExecuteNonQuery

and not the scalar.

2. You need to provide the datatypes of Va1-Val5.
 
L

Liz

Did you really this code with the two "INSERT INTOs" :

String insStr = "INSERT INTO INSERT INTO tbltest ..."

?
 
J

JamesB

sloan said:
Check
http://www.4guysfromrolla.com/webtech/chapters/ASPNET/ch06.2.shtml
Listing 6.2.4 Adding a Record to Access


PS

YOu have a DOUBLE "insert into insert into"
The double insert into was just a "copy + paste into my news post" cockup :)

The site you gave me is handy, but certainly looks like I am doing it
correctly.
I have another table with less fields (and no "Yes/No" one) and that
works... so perhaps I need different syntax for that bit...
 
S

sloan

try a

'true'
or
'false'

also remember that Access is
0
-1

and not
0
1

for false / true.
 

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

Similar Threads

error in code 2
Problem running query in code 8
SQL Insert SYntax 3
SQL Statement to XLS 3
LDAP query error 5
Write to closed workbook code 4
Newbie: Help with INSERT error message 4
SQL statement in Access 4

Top