8

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!

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Expert wanna be
  • 10,218
  • 26
  • 105
  • 158
  • Use `==` or `!=` not `.Equals`. Also, you may need to be checking against `DBNull`, not just `null`. (Yes, they're different).. – Servy Apr 19 '12 at 19:43
  • Possible of duplicate? http://stackoverflow.com/questions/3432974/datareader-is-null-or-empty – King Chan Apr 19 '12 at 19:49

6 Answers6

11

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.

phoog
  • 42,068
  • 6
  • 79
  • 117
Sandeep
  • 7,156
  • 12
  • 45
  • 57
  • 3
    +1 for actually explaining something. I took the liberty of editing it a bit; please roll back any changes you disagree with. – phoog Apr 19 '12 at 20:02
5

Use DbNull:

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

Sunny
  • 6,286
  • 2
  • 25
  • 27
4

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.

medkg15
  • 1,565
  • 15
  • 13
2
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;
phoog
  • 42,068
  • 6
  • 79
  • 117
1

Try the following:

DateTime flsdate = reader["fdate"] != null && reader["fdate"] != System.DbNull.Value
    ? DateTime.ParseExact(reader["fdate"]) 
    : DateTime.Today;
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
1
DateTime flsdate = DateTime.Today;
if(reader["fdate"] != null)
    flsdate = Convert.ToDateTime(reader["fdate"])
Forte L.
  • 2,772
  • 16
  • 25