Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store checkbox to Database Yes/No Field

Status
Not open for further replies.

garak76

Programmer
Sep 10, 2003
5
DE
Hi,

I receive an error when I try to store a checkbox value
to my MS-SQL Database Yes/No Field.
I assign the field with the following code:

row["INT_Height"] = txtHeight.Text;
row["BOL_Jump"] = chkJump.Checked;
...
dataAdapter.Update(ds, "tbl_Root_Characters");

I get the error:
ERROR: Syntaxerror converting varchar value 'True' to a row of type bit

can anybody help
thx
Frank
 
It's literally trying to save 'TRUE' to a bit field 1 or 0. You will need to convert it to either a 1 or 0 before your update.
 
I also tried this:

row["BOL_Jump"] = 1;

But my try.. catch... block gets the same error
and data is not stored.

[...]
row["BOL_Jump"] = 1;
row.EndEdit();

try
{
dataAdapter.Update(ds, "tbl_Root_Characters");
ds.AcceptChanges();
}
catch( Exception ex )
{
System.Console.WriteLine( "READING:" );
System.Console.WriteLine( " ERROR:" + ex.Message );
} }
 
wsmall73 is right.
Here is an example that is working 100%!
In MS - SQL there is no Yes/No data type. The type should be bit and the values are 1 or 0.
When you load the source database into a DataSet object a Yes/No source field is stored in a field of a DataTable object as DataType of System.Boolean and you should check it like here:
DataRow drFound = ...;
if drFound["myCheckColumn"].ToString().ToLower()== "true")
{
// get from database 1
}
else
// get from databse 0

If you want to alter the the myckeckcolumn you have to say:
drFound["mycheckColumn"]= 1;
or
drFound["mychcekColumn"] = 0;

Now you can call :
myDataAdapter.Update(ds, "myTable");
ds.Tables["myTable"].AcceptChanges(); // "myCheckColumn" is part of the "myTable"

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top