Thursday, 5 September 2013

Proper use of DBNull

Proper use of DBNull

Can someone inform me of the proper use of DBNull? I'm developing an MVC3
project which has this query in the controller.
I have a linq query that returns a field that I know has no value on the
first cycle:
var qryGetBirthdate = (from c in db.Customer
select new
{
c.Birthdate
}
).FirstOrDefault();
In the database, Birthdate is a Datetime type which is null.
The check I do for this is wrong:
if ( !DBNull.Value.Equals(qryGetBirthdate.Birthdate))
{
DateTime? dob = qryGetBirthdate.Birthdate;
}
I've tried variations from other blogs but can't find the answer.
Basically what I want is to catch the fact that
'qryGetBirthdate.Birthdate' doesn't exist yet. All I'm getting is the
exception that the object isn't set to a reference which I know.
What am I doing wrong?

No comments:

Post a Comment