Expert wanna be
Expert wanna be

Reputation: 10624

C# Object null check

I read my database using DataReader.

and some row does not have fdate value.

so when I Convert the null date to DateTime then Error occurs.

How can I check the field empty or not?

AdsCommand cmd = conn.CreateCommand();
cmd.CommandText = "select name,fdate from abc";

AdsDataReader reader = cmd.ExecuteReader();

DateTime flsdate = (reader["fdate"].Equals(null))? Convert.ToDateTime(reader["fdate"]) : DateTime.Today;

I tried with Equals, but it does not work.

anybody know how to check the null object to avoid convert error?

Thank you!

Upvotes: 8

Views: 5520

Answers (6)

Sandeep
Sandeep

Reputation: 7334

As everyone pointed you how to solve the issue, I am trying to give you the info regarding what is the difference between NULL and DBNull.

  • null and DBNull are different.

  • null is not an instance of any type. DBNull is a singleton class with one instance: DBNull.Value.

  • null represents an invalid reference where as DBNull.Value represents a nonexistent value in DB.

  • DBNull.Value is what db providers provide for a nonexistant value in a table.

With that background (reader["fdate"].Equals(null)) is not correct to use here. You have to check it with DBNull.Value. If it is of type DBNull, or if it is equal to DBNull.Value, then assign what ever value you like.

Upvotes: 11

medkg15
medkg15

Reputation: 1595

In a situation such as this I like to represent nullable database columns with either a reference type (string for varchar) or a Nullable wrapped value type (DateTime?). This way you're more accurately representing the database schema in your program.

This also allows you to more cleanly write the conversion logic using the format:

DateTime? fdate = datareader["fdate"] as DateTime?;

This cast will fail in the event the datareader result is a DbNull and fdate will be set to default(DateTime?), which is null. At that point you can obtain your real desired value by checking whether the nullable type has a value or not (fdate.HasValue), and if not, using your default - DateTime.Today.

Upvotes: 4

Forte L.
Forte L.

Reputation: 2812

DateTime flsdate = DateTime.Today;
if(reader["fdate"] != null)
    flsdate = Convert.ToDateTime(reader["fdate"])

Upvotes: 1

Sunny
Sunny

Reputation: 6336

Use DbNull:

http://forums.asp.net/t/1383849.aspx/1

Upvotes: 5

Mathew Thompson
Mathew Thompson

Reputation: 56429

Try the following:

DateTime flsdate = reader["fdate"] != null && reader["fdate"] != System.DbNull.Value
    ? DateTime.ParseExact(reader["fdate"]) 
    : DateTime.Today;

Upvotes: 1

phoog
phoog

Reputation: 43036

DateTime flsdate = reader["fdate"].Equals(DBNull.Value)
    ? Convert.ToDateTime(reader["fdate"])
    : DateTime.Today;

But it seems dangerous to default the date to Today. I'd do this instead:

DateTime? flsdate = reader["fdate"].Equals(DBNull.Value)
    ? Convert.ToDateTime(reader["fdate"])
    : (DateTime?)null;

Furthermore, if the underlying tpe of the fdate column is already DateTime, don't use System.Convert:

DateTime? flsdate = reader["fdate"].Equals(DBNull.Value)
    ? (DateTime?)reader["fdate"])
    : null;

Upvotes: 2

Related Questions