Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JamesB - 03 Mar 2008 21:59 GMT
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...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.