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

9

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.

Immortality answered 19/1, 2011 at 19:19 Comment(0)
S
34
var ordinal = rdr.GetOrdinal("timeOut");
if(rdr.IsDBNull(ordinal)) {
  queryResult.Egresstime = "Logged in";
} else {
  queryResult.Egresstime = rdr.GetString(ordinal);
}//if

or

if(Convert.IsDBNull(rdr["timeOut"])) {
  queryResult.Egresstime = "Logged in";
} else {
  queryResult.Egresstime = rdr.GetString("timeOut");
}//if
Shovelhead answered 19/1, 2011 at 19:39 Comment(3)
You are the Winner 0f your very own green check. I used the second one. Thanks!Immortality
small typo in the in second example: change "IsDbNull" to "IsDBNull"Dreadfully
This is crazy long for a null check.Slick
R
14

if(rdr.GetString("timeOut") == DBNull.Value)

null is not the same as DBNull

I am sorry, wrong answer, Sam B is right. I mistook this for DataRow stuff.

SqlDataReader does have strongly typed GetString() and provides IsDBNull(int column) for this case.

Rationalism answered 19/1, 2011 at 19:20 Comment(2)
I think it should be DBNull.ValueClaar
Heres what I get Error 1:Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull'Immortality
T
4

You must call rdr.IsDBNull(column) to determine if the value is DbNull.

Tokyo answered 19/1, 2011 at 19:35 Comment(0)
P
2

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

Pullet answered 11/1, 2013 at 10:40 Comment(0)
L
1

Change null to DBNull.Value.

Lacquer answered 19/1, 2011 at 19:21 Comment(1)
Heres what I get Error 1:Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull' –Immortality
C
1

You can also do:

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

Claar answered 19/1, 2011 at 19:23 Comment(3)
Has the same effect as (rdr.GetString("timeOut") == null)Immortality
@rd42: so what is the actual value in that field when you debug? just curious?Claar
The value is: 12/7/2010 10:16:46 AM Thanks for your help.Immortality
M
1

Here's one I like:

var MyString = rdr["column"] is DBNull ? "It's null!" : rdr.GetString("column");

E.g. (for the original requirement):

queryResult.Egresstime = rdr["timeOut"] is DBNull ? "Logged in" : rdr.GetString("timeOut");
Misdeal answered 25/11, 2013 at 21:14 Comment(0)
S
1
    private T GetNullableValue<T>(MySqlDataReader rdr, string parameterName)
    {
        object value = rdr[parameterName];
        if (value is DBNull)
            return default;

        return (T)value;
    }

And the usage for example:

string message = GetNullableValue<string>(rdr, "Message");
bool flag = GetNullableValue<bool>(rdr, "Flag");
DateTime startTime = GetNullableValue<DateTime>(rdr, "StartTime");
Slaby answered 14/3, 2021 at 13:44 Comment(0)
B
0

Here is a method that I created to read DBNull and return a default(T) incase:

   private T GetNullable<T>(MySqlDataReader reader, int ordinal, Func<int, T> getValue)
        {
            if (reader.IsDBNull(ordinal))
            {
                return default(T);
            }
            return getValue(ordinal);
        }

It can be used like this:

   if (reader.Read())
            {
                account = new Account();
                account.Id = reader.GetInt32(0);
                account.Name = reader.GetString(1);
                account.MailVerifiedAt = GetNullable(reader, 2, reader.GetDateTime);
                account.MailToken = GetNullable(reader, 3, reader.GetString);
            }

The generic type T will be resolved based on the return value of the reader.- method. If it returns a string you will receive a null incase of DBNull. If it is an int it will return 0, etc.

Note: for integer values it might not be desired to get a 0 so be careful.

Baulk answered 8/8, 2018 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.