Link to home
Start Free TrialLog in
Avatar of keerthimothukuri
keerthimothukuriFlag for United States of America

asked on

Which should be used when comparing an object with null- Null or DBNull.Values

I have stored procedure which returns a table - dt. one of the columns in the table is a date column.
I need to write a condition wherein I need to check if the value is not equal to null

foreach (DataRow dr in dt.Rows)
                {
// should I use dr[SendDate]!=null or dr  [SendDate]!=DBnull.value. which is correct in this scenario? (senddate is a datetime type column)
if ((dr["SENDDATE"] != DBNull.Value)                    
{
}
}

Please let me know hich is correct to user dr[Sendate]!=null or dr[Sendate]!=DBnull.Value
ASKER CERTIFIED SOLUTION
Avatar of ladarling
ladarling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ladarling is correct. You want to compare to DBNull, not null.

The syntax I would use is this, as you loop through the results of the SqlDataReader returned by a call to ExecuteReader():

if (!reader.IsDBNull(reader.GetOrdinal("SENDDATE")))
{
}

Some of that is my personal preference, but DBNull is definitely what you are trying to compare to.