简体   繁体   中英

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!

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.

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.

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;

Try the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM