I have the following function in C#, that I want to pass a MySQL DataReader value/object.
If the value of the object passed is a DBNull I need to handle it and return a Null value of that data type e.g null int.
Otherwise I want to convert the returned value like Convert.ToInt32(value) or similar. My function is;
public static T ConvertFromDB<T>(object value)
{
return value == DBNull.Value ? default(T) : (T)value;
}
I can call the function like
int MyInt = ConvertFromDB(oRes["FieldName"]);
However in this case the MySQLDataReader result for oRes["FieldName"] is a DBNull, which causes a exception.
Can anyone offer any help on this ? I am using strongly typed columns, but the problem I have is if the DB value is DBNull, I need to return null of that data type e.g (int)null