 |
BOOK: Beginning Visual C#  | This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 20th, 2004, 05:32 PM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
A SQL problem (in my C# program!)
I am having a tough time finding a way to pass Checkbox values back to an ACCESS database table(expects yes/no). I get an invalid data type error. Can somebody out there please inform me how to do this procedure?
Here is the code I am using:
OdbcConnection myConn = new OdbcConnection(@"Provider=MSDASQL;Driver={Microsof t Access Driver (*.mdb)};DBQ=C:\Addresses.mdb");
string myInsertQuery = "INSERT INTO addresses(Last_Name, First_name, Email_address, Comments, trips, bracket, photos) " +
"VALUES ( '" + txtLastName.Text + "', '" + txtFirstName.Text + "', '" + txtEmailAddress.Text + "', '" + txtComments.Text + "', '" + chkTravel.CheckState + "', '" + chkFlashBracket.CheckState + "', '" + chkPhotographs.CheckState + "')";
OdbcCommand myOdbcCommand = new OdbcCommand(myInsertQuery);
myOdbcCommand.Connection = myConn;
myConn.Open();
myOdbcCommand.ExecuteNonQuery();
myOdbcCommand.Connection.Close();
There are three checkboxes involved:
chkTravel, chkFlashBracket, chkPhotographs. If I replace these with the values 0 or 1, everthing works great.
Cmarek
__________________
Cmarek
|
|

September 20th, 2004, 08:10 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
Try the Checked property, instead of CheckState. That is a zero to one value.
Brian
|
|

September 20th, 2004, 10:02 PM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey Brian, that was actually the first way i tried it, sure seemed the way to go. But it gave me the following error: ERROR{22018][Microsoft][ODBC Microsoft Access Driver]Data type mismatch in criteria expression.
I have a way I am doing things for now, just to get it to work. Seems like a bit more effort than I should be required to do...I am calling a function - FindBoolValue(bool chkBox), that returns an int 0 or 1. I do this within the SQL building statement:
string myInsertQuery = "INSERT INTO addresses(Last_Name, First_name, Email_address, Comments, trips, bracket, photos) " +
"VALUES ( '" + txtLastName.Text + "', '" + txtFirstName.Text + "', '" + txtEmailAddress.Text + "', '" + txtComments.Text + "', '" + FindBoolValue(chkTravel.Checked) + "', '" + FindBoolValue(chkFlashBracket.Checked) + "', '" + FindBoolValue(chkPhotographs.Checked) + "')";
private int FindBoolValue(bool chkBox)
{
int YesNo = (chkBox == true) ? 1 : 0;
return YesNo;
}
Cmarek
|
|
 |