how to check if the field returned by a SELECT statement is null

F

Francois Malgreve

As the indexer (for C#, SqlDataReader.Item for VB) on the SqlDataReader
object returns an object of type Object, I need to do the following kind of
casting to get my value:

String value = (string) dataRead["FirstName"];
int age = (int) dataRead["age"];

The problem is that is the column in the DB contains a null value, the cast
will throw an exception as in that case the object returned by the indexer
is of type DbNull.

Then I do the following stuff to avoid casting excpetion:

String value = null;
if ( !(dataRead["FirstName"] is DBNull) )
{
value = (string) dataRead["FirstName"];
}

he problem of this way of doing is that, IMHO, doing a type check may be
performance costing especially as I will have tons of them as I need to do
it for any DB field I retrieve from the DB.

As my goal is just to know if the DB field contain a value or is NULL, then
my question is:
Is there a more efficient way to achieve the same goal than by checking the
type?

Thanks in advance

Francois
 
M

Miha Markic

Hi Francois.

Yes, there is a faster way (typecasting is expensive):

SqlDataReader.IsDBNull method.
 
F

Francois Malgreve

Thank you very much for this. One limitation is that it is using the index
of the column only and not the column name. But that is ok.

However I wonder about the way Microsoft implemented the method
SqlDataReader.IsDBNull(int i)
Maybe it is doing the same thing as what I did "manually"?

How can we check how a specific method of the frameowrk is implemented? No
source code? In Java I used to look at the code of the Java libraries to
have better control and understanding on what happens under the bush... is
it possible to do that in .NET ?

Thanks again.

Francois


Miha Markic said:
Hi Francois.

Yes, there is a faster way (typecasting is expensive):

SqlDataReader.IsDBNull method.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

Francois Malgreve said:
As the indexer (for C#, SqlDataReader.Item for VB) on the SqlDataReader
object returns an object of type Object, I need to do the following kind of
casting to get my value:

String value = (string) dataRead["FirstName"];
int age = (int) dataRead["age"];

The problem is that is the column in the DB contains a null value, the cast
will throw an exception as in that case the object returned by the indexer
is of type DbNull.

Then I do the following stuff to avoid casting excpetion:

String value = null;
if ( !(dataRead["FirstName"] is DBNull) )
{
value = (string) dataRead["FirstName"];
}

he problem of this way of doing is that, IMHO, doing a type check may be
performance costing especially as I will have tons of them as I need to do
it for any DB field I retrieve from the DB.

As my goal is just to know if the DB field contain a value or is NULL, then
my question is:
Is there a more efficient way to achieve the same goal than by checking the
type?

Thanks in advance

Francois
 
M

Miha Markic

Hi,
However I wonder about the way Microsoft implemented the method
SqlDataReader.IsDBNull(int i)
Maybe it is doing the same thing as what I did "manually"?

It is comparing the value and not typecasting it.
How can we check how a specific method of the frameowrk is implemented? No
source code? In Java I used to look at the code of the Java libraries to
have better control and understanding on what happens under the bush... is
it possible to do that in .NET ?

You are right - the best method to understand what's going on.
Yes, there is a method. Use a decompiler such as
Reflector: http://www.aisto.com/roeder/dotnet/
Anakrino: http://www.saurik.com/net/exemplar/

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.
 
F

Francois Malgreve

Sorry I may sound a little bit dumb but what do you mean exactly by
comparing value ?

I think about something like using Object.GetType() on both objects and then
comparing the Type.FullName property of both object. Is it what you mean?

If not and if you have time to enlight this point about comparing values
that would really help me a lot.

Thank you very much again.

Francois
 
M

Miha Markic

Francois Malgreve said:
Sorry I may sound a little bit dumb but what do you mean exactly by
comparing value ?

Something like:
reader["column"] == null || reader["column"] == DBNull.Value
I think about something like using Object.GetType() on both objects and then
comparing the Type.FullName property of both object. Is it what you mean?

Nope - that is types comparing.
If not and if you have time to enlight this point about comparing values
that would really help me a lot.

Sure, no problem. Look above. If you still miss something, just ask.
BTW, you might try one of the decompilers and see by yourself the IsDBNull
method.
 
F

Francois Malgreve

Ok now all of this is pretty clear. Thank you very much for your patience
and clear explanation.

I will have a look at the decompilers. I will have a try to decompile the
..net source. Would be easier if we could directly get the code with all the
comments and things but seems like it will never be the case with MS.

Best regards,

Francois


Miha Markic said:
Francois Malgreve said:
Sorry I may sound a little bit dumb but what do you mean exactly by
comparing value ?

Something like:
reader["column"] == null || reader["column"] == DBNull.Value
I think about something like using Object.GetType() on both objects and then
comparing the Type.FullName property of both object. Is it what you
mean?

Nope - that is types comparing.
If not and if you have time to enlight this point about comparing values
that would really help me a lot.

Sure, no problem. Look above. If you still miss something, just ask.
BTW, you might try one of the decompilers and see by yourself the IsDBNull
method.
 
M

Miha Markic

Francois Malgreve said:
Ok now all of this is pretty clear. Thank you very much for your patience
and clear explanation.

You're welcome.
I will have a look at the decompilers. I will have a try to decompile the
.net source. Would be easier if we could directly get the code with all the
comments and things but seems like it will never be the case with MS.

Sure, it would be eaiser.
There is Rotor which comes with full sources if you are interested.
http://research.microsoft.com/Collaboration/University/Europe/RFP/Rotor/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top