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?
sloan - 03 Mar 2008 22:11 GMT
A.
You want to run
.ExecuteNonQuery
and not the scalar.
2. You need to provide the datatypes of Va1-Val5.
>I am trying to insert a row to an Access database:
>
[quoted text clipped - 16 lines]
>
> Anyone spot what I'm doing wrong here?
sloan - 03 Mar 2008 22:17 GMT
OOPS.
You gave us the data types. Sorry.
> A.
>
[quoted text clipped - 25 lines]
>>
>> Anyone spot what I'm doing wrong here?
Liz - 03 Mar 2008 22:49 GMT
>>I am trying to insert a row to an Access database:
>>
[quoted text clipped - 6 lines]
>> Val5) VALUES ('blah', 0, '', '', 'foo');
>> OleDbCommand insCmd = new OleDbCommand(insStr, DBConn);
Did you really this code with the two "INSERT INTOs" :
String insStr = "INSERT INTO INSERT INTO tbltest ..."
?
>> {
>> insCmd.ExecuteScalar();
[quoted text clipped - 5 lines]
>>
>> Anyone spot what I'm doing wrong here?
sloan - 03 Mar 2008 22:21 GMT
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"
>I am trying to insert a row to an Access database:
>
[quoted text clipped - 16 lines]
>
> Anyone spot what I'm doing wrong here?
JamesB - 04 Mar 2008 18:42 GMT
> Check
> http://www.4guysfromrolla.com/webtech/chapters/ASPNET/ch06.2.shtml
[quoted text clipped - 3 lines]
>
> 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...
sloan - 05 Mar 2008 14:51 GMT
try a
'true'
or
'false'
also remember that Access is
0
-1
and not
0
1
for false / true.
>> Check
>> http://www.4guysfromrolla.com/webtech/chapters/ASPNET/ch06.2.shtml
[quoted text clipped - 11 lines]
> I have another table with less fields (and no "Yes/No" one) and that
> works... so perhaps I need different syntax for that bit...