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.
nulland DBNull are different.nullis not an instance of any type. DBNull is a singleton class with one instance:DBNull.Value.nullrepresents an invalid reference where asDBNull.Valuerepresents a nonexistent value in DB.DBNull.Valueis 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 withDBNull.Value. If it is of typeDBNull, or if it is equal toDBNull.Value, then assign what ever value you like.