Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 599
Expert wanna be Avatar asked Apr 19 '12 19:04

Expert wanna be


5 Answers

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.

like image 78
Sandeep Avatar answered Oct 19 '22 00:10

Sandeep


Use DbNull:

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

like image 30
Sunny Avatar answered Oct 18 '22 22:10

Sunny


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.

like image 40
medkg15 Avatar answered Oct 18 '22 23:10

medkg15


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;
like image 34
phoog Avatar answered Oct 18 '22 22:10

phoog


Try the following:

DateTime flsdate = reader["fdate"] != null && reader["fdate"] != System.DbNull.Value
    ? DateTime.ParseExact(reader["fdate"]) 
    : DateTime.Today;
like image 40
mattytommo Avatar answered Oct 18 '22 22:10

mattytommo