Boardy
Boardy

Reputation: 36205

MySQLDataReader retrieving Null value problem in c#

I am currently working on a C# project that will export MySQL Data. The export is for any database within the server so I am not going to know what fields and the data types that are in the table and I am not going to know if a field in the table allows null values or not.

During testing, I have found that the export is working fine but if the field allows null when the mysql data reader goes gets to the row which is null it displays an error SqlNullValueException, data is null.

I have tried doing if (reader.getString(field) == null) {} but it is still displaying the error.

How do I cope with Null values in the database.

Thanks for any help you can provide.

Upvotes: 11

Views: 26865

Answers (7)

Greg Gum
Greg Gum

Reputation: 37875

Expanding on @Summer-Time answer to include int columns:

using System.Data;

namespace MyApp
{

    public static class AppExtensions
    {
        public static string GetStringSafe(this IDataReader reader, int colIndex)
        {
            return GetStringSafe(reader, colIndex, string.Empty);
        }

        public static string GetIntAsStringSafe(this IDataReader reader, int colIndex, string defaultValue)
        {
            if (!reader.IsDBNull(colIndex))
                return reader.GetInt32(colIndex).ToString();
            else
                return defaultValue;
        }

        public static string GetIntAsStringSafe(this IDataReader reader, int colIndex)
        {
           return GetIntAsStringSafe(reader, colIndex, string.Empty);
        }


        public static string GetStringSafe(this IDataReader reader, int colIndex, string defaultValue)
        {
            if (!reader.IsDBNull(colIndex))
                return reader.GetString(colIndex);
            else
                return defaultValue;
        }

        public static string GetStringSafe(this IDataReader reader, string indexName)
        {
            return GetStringSafe(reader, reader.GetOrdinal(indexName));
        }

        public static string GetStringSafe(this IDataReader reader, string indexName, string defaultValue)
        {
            return GetStringSafe(reader, reader.GetOrdinal(indexName), defaultValue);
        }
    }
}

Upvotes: 0

Oscar
Oscar

Reputation: 11

You could always use the c# conditional operator '?' like so...

string val = (reader.IsDBNull(columnIndex)) ? "" : reader.GetString(columnIndex);

Upvotes: 1

Illuminator
Illuminator

Reputation: 565

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

Upvotes: 0

Summer-Time
Summer-Time

Reputation: 1874

In this blog Post there is an good extension method for the reader

SQL Data Reader - handling Null column values

I Changed it to the IDataReader Interface

public static string GetStringSafe(this IDataReader reader, int colIndex)
    {
        return GetStringSafe(reader, colIndex, string.Empty);
    }

    public static string GetStringSafe(this IDataReader reader, int colIndex, string defaultValue)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetString(colIndex);
        else
            return defaultValue;
    }

    public static string GetStringSafe(this IDataReader reader, string indexName)
    {
        return GetStringSafe(reader, reader.GetOrdinal(indexName));
    }

    public static string GetStringSafe(this IDataReader reader, string indexName, string defaultValue)
    {
        return GetStringSafe(reader, reader.GetOrdinal(indexName), defaultValue);
    }

Upvotes: 4

sithius92
sithius92

Reputation: 136

I have had problems with using the GetString() method on fields that allow null values. I worked around this by doing something like:

reader[0].ToString()

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

.Net does not use a null literal to distinguish database nulls. I can only speculate, but I suspect the reason for this is that many common database column types (int, float, etc) are value types, and comparing a value type to null won't work at all how you expect.

Instead, check for DBNull.Value or use the .IsDbNull() function.

Upvotes: 2

Deleted
Deleted

Reputation: 4988

You need to test for null explicitly in a reader so:

if (!reader.IsDbNull(field)) {
    var value = reader.GetString(field);
    // ... do stuff here ...
}

Upvotes: 24

Related Questions