Useful data layer functions
Published on 1/2/2008 1:00:00 PM by Philip
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;
}
0 Comments
Tags:
Bookmark and Share
Design downloaded from Free Templates - your source for free web templates