Reputation: 4753
i need to fetch some records from database and then bind it to a grid view.
But , in the data i am fetching from DB, there are some null values
So, Inorder to avoid type casting error , i am using a function to avoid cast error.
public static T GetValue<T>(object o)
{
T val = default(T);
if (o != null && o != DBNull.Value)
{
val = (T)o;
}
return val;
}
But, when i am binding to grid , it is showing as o for columns of type long . But, i need
to show as no value or nothing . Is it possible , if so please give your sugesstions
Upvotes: 0
Views: 605
Reputation: 40393
Looks like you just need to use long?
instead of long
as your generic parameter. I'm assuming you're currently doing something like:
long val = GetValue<long>(someDataField);
Just change that up to use the Nullable<long>
type, and you'll get null
back, and that should work in your grid.
long? val = GetValue<long?>(someDataField);
Upvotes: 2