Handle Null in Datareader

SQL Data Reader - handling Null column values

You need to check for IsDBNull:

if(!SqlReader.IsDBNull(indexFirstName))
{
employee.FirstName = sqlreader.GetString(indexFirstName);
}

That's your only reliable way to detect and handle this situation.

I wrapped those things into extension methods and tend to return a default value if the column is indeed null:

public static string SafeGetString(this SqlDataReader reader, int colIndex)
{
if(!reader.IsDBNull(colIndex))
return reader.GetString(colIndex);
return string.Empty;
}

Now you can call it like this:

employee.FirstName = SqlReader.SafeGetString(indexFirstName);

and you'll never have to worry about an exception or a null value again.

Handle NULL in Datareader

You can use SqlDataReader.IsDBNull to check for null values out of a data reader. C# null and DBNull are different.

 c.ActualWeight = 
dr.IsDBNull(0)
? default(float)
: float.Parse(dr[0].ToString().Trim());

how to check if a datareader is null or empty

if (myReader["Additional"] != DBNull.Value)
{
ltlAdditional.Text = "contains data";
}
else
{
ltlAdditional.Text = "is null";
}

data is null when using SqlDataReader

You need to check the nullable fields using the SqlDataReader.IsDBNull method:

int statusIndex = Reader.GetOrdinal("status");
string sstatus = Reader.IsDBNull(statusIndex) ? null : Reader.GetString(statusIndex);

How to use datareader with null values

It seems to me that you need a conversion like this (using an extension method for convenience):

public static int? ToNullableInt32(this SqlInt32 value)
{
return value.IsNull ? (int?) null : value.Value;
}

Then:

Field2 = rdr.GetSqlInt32(Field2_Ordinal).ToNullableInt32()

(Comment on other answers: there's no need to bring DbNull into this, as SqlInt32 can already represent null values. You just need to detect that before using Value.)

Handling null values from a datareader to a subroutine

VB.NET dates are a bit funky when handling a NULL date from a DB. First, to handle reading a date with a NULL value from the database into your VB.NET Date variable:

Dim myDate As Date = If(dr("MyDate") Is DBNull.Value, Nothing, dr("MyDate")) 

Note that setting a Date type to Nothing gives it a value of #12:00:00# (the minimum value for a Date data type), and not Nothing as in a null object. Which means that you check a date for Nothing to send a NULL to the database like this:

cmd.Parameters("@MyDate").Value = If(.myDate = Nothing, DBNull.Value, .myDate)

Note that we don't use If(.myDate Is Nothing,...

Handle NULL values when reading through OracleDataReader?

yos.ServiceCredited = odr.IsDBNull(1) ? 0 : odr.GetDecimal(1);

OracleDataReader provides a IsDBNull() method.

And the docs on GetDecimal() ask us to do this

Call IsDBNull to check for null values before calling this method.

SqlDataReader Best way to check for null values -sqlDataReader.IsDBNull vs DBNull.Value

I would not get too caught up in the which method is better, because both work and I have used both in code before.

For instance, here is a utility function I dug up from one of my old projects:

/// <summary>
/// Helper class for SqlDataReader, which allows for the calling code to retrieve a value in a generic fashion.
/// </summary>
public static class SqlReaderHelper
{
private static bool IsNullableType(Type theValueType)
{
return (theValueType.IsGenericType && theValueType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
}

/// <summary>
/// Returns the value, of type T, from the SqlDataReader, accounting for both generic and non-generic types.
/// </summary>
/// <typeparam name="T">T, type applied</typeparam>
/// <param name="theReader">The SqlDataReader object that queried the database</param>
/// <param name="theColumnName">The column of data to retrieve a value from</param>
/// <returns>T, type applied; default value of type if database value is null</returns>
public static T GetValue<T>(this SqlDataReader theReader, string theColumnName)
{
// Read the value out of the reader by string (column name); returns object
object theValue = theReader[theColumnName];

// Cast to the generic type applied to this method (i.e. int?)
Type theValueType = typeof(T);

// Check for null value from the database
if (DBNull.Value != theValue)
{
// We have a null, do we have a nullable type for T?
if (!IsNullableType(theValueType))
{
// No, this is not a nullable type so just change the value's type from object to T
return (T)Convert.ChangeType(theValue, theValueType);
}
else
{
// Yes, this is a nullable type so change the value's type from object to the underlying type of T
NullableConverter theNullableConverter = new NullableConverter(theValueType);

return (T)Convert.ChangeType(theValue, theNullableConverter.UnderlyingType);
}
}

// The value was null in the database, so return the default value for T; this will vary based on what T is (i.e. int has a default of 0)
return default(T);
}
}

Usage:

yourSqlReaderObject.GetValue<int?>("SOME_ID_COLUMN");
yourSqlReaderObject.GetValue<string>("SOME_VALUE_COLUMN");


Related Topics



Leave a reply



Submit