A friend, Rob, and I wrote these functions today. You may find them helpful when coverting null values from stored procedures or datatables/datarows in your data layer.
/// <summary>
/// This method compares value with DBNull.Value and returns null or the value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T? GetValueTypeValue<T>(object value) where T : struct
{
if (value == DBNull.Value)
return null;
else
return (T)value;
}
/// <summary>
/// This method compares value with DBNull.Value and returns null or the value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T GetReferenceTypeValue<T>(object value) where T : class
{
if (value == DBNull.Value)
return null;
else
return (T)value;
}