473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

nullable type cast question

DMG
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);
Thanks in advance for any help.

Oct 25 '06 #1
2 1837
Hi,

dtr["isTestField"] is probably returning DBNull.

Try the following instead:

bool? bTest;
if (dtr["isTestField"] is DBNull)
bTest = null;
else
bTest = (bool) dtr["isTestField"];

--
Dave Sexton

"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);
Thanks in advance for any help.

Oct 25 '06 #2
"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
Oct 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in...
4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
5
by: Narshe | last post by:
How can I check if a type is nullable? If a type is created like bool? = null;, then how can you tell if the type is nullable as opposed to not? You have to cast from bool to bool? and such, so...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
7
by: tshad | last post by:
Can you handle nullable string vb.net 2.0? You can with Integer but I get an error when I try to do a Dim s as Nullable (Of String) Now strings are a nullable type but it can't handle: ...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
1
by: Tony | last post by:
Hello! This construction int? op1 = 5; int result = op1 * 2; gived a compile error with Error 1 Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you...
3
by: Tony Johansson | last post by:
Hello! Is it possible to declare existing .net generics to have nullable types(for example type int? ) ? If the answer on the previous question is yes then I assume that you can also define...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.