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