Yo,

Got a checkbox which I databind at runtime to a numeric column. It works
fine but when I add a new record I get the error:

--------------------------------
An unhandled exception of type 'System.InvalidCastException' occurred in
mscorlib.dll

Additional information: Object cannot be cast from DBNull to other types.
--------------------------------

I'm databinding just after adding a new record, so the order of events is:

1. Add new record code:
private void AddRecord()
{
// daBuilding is my DataAdapter, dsBuilding is my DataSet.
daBuilding.FillSchema(dsBuilding, System.Data.SchemaType.Source,
"BUILDING");

this.BindingContext[dsBuilding, "BUILDING"].AddNew();
this.DataBindControls();
}

2. Databinding code:
private void DataBindControls()
{
// Check the global flag to see if we have already databound controls.
if (this.dataBound)
return;
.
.
// this is the line that craps out:
chkHeritage.DataBindings.Add("Checked", this.dsBuilding,
"BUILDING.HERITAGE_LISTED_YN");
chkHeritage.DataBindings["Checked"].Format += new
ConvertEventHandler(NullToBoolean);
.
.
// Set global flag so we don't have to databind again.
this.dataBound = true;
}


My understanding is that you have to databind AFTER having retrieved a
dataset to bind to, hence the FillSchema() call at the begining. But how
does this work when you add a new record and have checkBoxes which don't
know what to do with a null?

Any advice on this greatly appreciated.

Cheers,
PeterZ

Re: DBnull and checkBox when adding new record by Cowboy

Cowboy
Wed Jun 02 07:56:52 CDT 2004

This is one area where strongly typed datasets save you a lot of time. In a
strongly typed dataset, you can check for a null.

if(!ds.MyTable.IsCheckBox1Null)
{
}

Overall, a field that links to a checkbox should always have a 0 or a 1 (bit
field in SQL Server). If you are allowing nulls, you are defeating the idea
of setting up a Boolean control (checkbox can either be checked or not, and
not in some "unknown" state, which is what Null represents). If you ask the
question Do you want our email letter? The answer is either yes or no, not
NULL.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"PeterZ" <_RemoveTheSpammer_pz0071@hotmail.com> wrote in message
news:40bbda34$0$3036$afc38c87@news.optusnet.com.au...
> Yo,
>
> Got a checkbox which I databind at runtime to a numeric column. It works
> fine but when I add a new record I get the error:
>
> --------------------------------
> An unhandled exception of type 'System.InvalidCastException' occurred in
> mscorlib.dll
>
> Additional information: Object cannot be cast from DBNull to other types.
> --------------------------------
>
> I'm databinding just after adding a new record, so the order of events is:
>
> 1. Add new record code:
> private void AddRecord()
> {
> // daBuilding is my DataAdapter, dsBuilding is my DataSet.
> daBuilding.FillSchema(dsBuilding, System.Data.SchemaType.Source,
> "BUILDING");
>
> this.BindingContext[dsBuilding, "BUILDING"].AddNew();
> this.DataBindControls();
> }
>
> 2. Databinding code:
> private void DataBindControls()
> {
> // Check the global flag to see if we have already databound controls.
> if (this.dataBound)
> return;
> .
> .
> // this is the line that craps out:
> chkHeritage.DataBindings.Add("Checked", this.dsBuilding,
> "BUILDING.HERITAGE_LISTED_YN");
> chkHeritage.DataBindings["Checked"].Format += new
> ConvertEventHandler(NullToBoolean);
> .
> .
> // Set global flag so we don't have to databind again.
> this.dataBound = true;
> }
>
>
> My understanding is that you have to databind AFTER having retrieved a
> dataset to bind to, hence the FillSchema() call at the begining. But how
> does this work when you add a new record and have checkBoxes which don't
> know what to do with a null?
>
> Any advice on this greatly appreciated.
>
> Cheers,
> PeterZ
>
>
>
>
>
>



Re: DBnull and checkBox when adding new record by pz0071

pz0071
Wed Jun 02 22:55:20 CDT 2004

I understand what you're saying, but what happens when you create a
new row in your table? All values on the new row are null, are they
not? Only after the user fills in the necessary information thorugh
the form will there be actual values.

Databinding through code is certainly a pain!


"Cowboy" <NoSpamMgbworld@comcast.netNoSpamM> wrote in message news:<O9ACUGKSEHA.2332@TK2MSFTNGP10.phx.gbl>...
> This is one area where strongly typed datasets save you a lot of time. In a
> strongly typed dataset, you can check for a null.
>
> if(!ds.MyTable.IsCheckBox1Null)
> {
> }
>
> Overall, a field that links to a checkbox should always have a 0 or a 1 (bit
> field in SQL Server). If you are allowing nulls, you are defeating the idea
> of setting up a Boolean control (checkbox can either be checked or not, and
> not in some "unknown" state, which is what Null represents). If you ask the
> question Do you want our email letter? The answer is either yes or no, not
> NULL.
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>

DBnull and checkBox when adding new record by mhnd_79

mhnd_79
Sat Jun 12 06:02:15 CDT 2004

Hi,
i think the prolem is in the dataset as i figured out
you have to set a default value for the boolean column
Example:
dataSet1.Tables[0].Columns[2].DefaultValue = true; // or false
as u wish

this code must be added before the addnew() method

regards,
Mouhannad


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com