How to handle null db fields in DAL

I'm creating a simple blog in asp.net 4 using sql server 2008.  I'm using books as a reference which include samples of business logic layer (BLL) and data access layer (DAL).

The table to store my posts has the expected items - title, body, etc.  Some of the fields can be null.  In my data access class, which maps the db fields to a "post" data entity, when I read a null db column into an enitity property, I get an invalid casting error unless I explicity handle nulls.

So, I'm now checking for null before setting the property if the field may be null, like this:

return new PostEntity(
   (int)reader["PostID"],
   (string)reader["Title"],
   reader["TopicID"] == DBNull.Value ? 0 : (int)reader["TopicID"],
   reader["CreatedDateTime"] == DBNull.Value ? new DateTime() :     (DateTime)reader["CreatedDateTime"],
   reader["Intro"] == DBNull.Value ? "" : (string)reader["Intro"],
........ //rest of code here

This code works.

This makes sense, but the books I'm using don't do this - even when using db fields which can be null.  They just read the values into the entity.  Now, I've not actually run their code, but assuming their code does work - how?  Is it not necessary to do what I'm doing to handle null db values? What's the "right" way to do this?  Is their a good example online of a solution?
RekksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Obadiah ChristopherCommented:
One thing that they might do is

use Convert.ToInt32 instead of(int) for casting. coz Convert is capable of handling nulls..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rameshrrCommented:
hope it will work,

To check nulls in C# you can use IsNullOrEmpty() of String class.
eg,
string.IsNullOrEmpty(reader["Intro"].ToString())
---> will return true, if the result is Null or empty, in other case, you can convert it into your target format by Convert.To.....() or your own customized code..

in your case,
int topicId = string.IsNullOrEmpty(reader["TopicID"].ToString()) ? 0 : (int)reader["TopicID"],
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.