Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
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
 
Old September 20th, 2004, 05:32 PM
Authorized User
 
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old September 20th, 2004, 08:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Try the Checked property, instead of CheckState. That is a zero to one value.

Brian
 
Old September 20th, 2004, 10:02 PM
Authorized User
 
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting existing program to SQL Express DB jscammell VB Databases Basics 2 April 19th, 2007 02:03 PM
changing sql query of crystal reports from program connect2sandep Crystal Reports 1 August 22nd, 2006 01:15 PM
how to program bakup in sql drachx SQL Server 2000 10 October 17th, 2004 08:10 PM
problem with Setup Program alyeng2000 VB.NET 2002/2003 Basics 0 November 28th, 2003 04:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.