Alex Rivera | Logout

How to check for NULL in MySqlDataReader by the column's name?

Asked 2011-01-19T19:19:47.490
20

How can I check for a NULL value in an open MySqlDataReader?

The following doesn't work; it's always hitting the else:

if (rdr.GetString("timeOut") == null)
{
    queryResult.Egresstime = "Logged in";
}
else
{
    queryResult.Egresstime = rdr.GetString("timeOut");
}

rdr.IsDbNull(int i) only accepts a column number, not name.

Edit
Report

2 Answers

2

You can compare the object that retrive from NULL field with DBNull.Value.

answered 2013-01-11T10:40:11.953
1

You can also do:

If (string.IsNullOrEmpty(rdr.GetString("timeOut"))

answered 2011-01-19T19:23:32.743

Your Answer