"DMG" <Da**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Why does this fail with a cast exception? The field is coming in from
the data base as null due some bad design on my part but I thought this
would allow me to test and set to false.
DataTableReader dtr = ds.CreateDataReader();
bool? bTest = ((bool?)dtr["isTestField"]??false);
Because the type of the column in the dataset is not bool?, nor does it have
the value null. It's of type DBNull and has the value DBNull.Value.
Use something like:
DBNull.Value.Equals(dtr["isTestField"]) ? false : (bool)dtr["isTestField"];
Unfortunately, they remembered to provide a string overload of the indexer,
but not a string overload of DataTableReader.IsDBNull. If you know the
column ordinal, you can use that instead of the column name and write
dtr.IsDBNull(iOrdinal) ? false : (bool)dtr["iOrdinal];
-cd