Store value in Yes/No field in Access using C#

I can see a number of issues with your statement.

string sQuery = @"UPDATE ch1 set ch1 = " + true ;

I don’t have MS-Access handy, but it seems that it uses -1 for “Yes”. So you could try:

string sQuery = @"UPDATE ch1 set ch1 = -1" ;

or if the boolean value is in a variable, say, foo:

string sQuery = @"UPDATE ch1 set ch1 = " + (foo ? -1 : 0) ;

This is not advised however, as commenter @Gord Thompson points out. A literal

string sQuery = @"UPDATE ch1 set ch1 = True" ;

or

string sQuery = @"UPDATE ch1 set ch1 = " + foo;

should work.

I notice that your error message reads

Syntax error in query expression ”True’;’.

with True between quotes, but also with a semicolon in there. It’s possible that the line that causes the error is different from what you gave us, perhaps it was

string sQuery = @"UPDATE ch1 set ch1 = " + true + ";";

or something like that? With a semicolon where SQL wasn’t expecting one?

There are some more issues. I notice that both the table name and the column name are ch1. While it is allowed that a table and a column have the same name, it doesn’t happen often. Are these really the right names?

Also, there is no WHERE clause here. In it’s current form, this statement will fill the entire column of the table with the same value, which may not be what you want.

Finally, the format

string sQuery = @"UPDATE ch1 set ch1 = " + something ;

is open to a well-known attack called SQL injection. You should read up on so-called “prepared statements” and use these. As a side benefit, this will also prevent some syntax errors in your SQL statements.

Leave a Comment