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

  • Thread starter Thread starter JamesB
  • Start date Start date
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?
 
A.

You want to run
..ExecuteNonQuery

and not the scalar.

2. You need to provide the datatypes of Va1-Val5.
 
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.
 
Did you really this code with the two "INSERT INTOs" :

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

?
 
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...
 
try a

'true'
or
'false'

also remember that Access is
0
-1

and not
0
1

for false / true.
 
Back
Top