Sai Avinash
Sai Avinash

Reputation: 4753

How to bind DBnull values to grid view?

I am facing struggle to bind values to grid which are retrieved from data base. I have a database column of type DateTime which is nullable. So, when I am trying to bind that null value, it is throwing an error while adding those column value to object property . So, before adding values fetched from database, i am using a function that converts the value to its default type before adding to object. Since , the default value for datetime Type is 1/1/0001 12:00:00 AM . So, where ever null values are present , I am getting this value for this field.

How to solve this issue? Please give your sugesstions.

To explain my scenario , i am adding a piece of code here.

public static T GetValue<T>(object o)
{
    T val = default(T);

    if (o != null && o != DBNull.Value)
    {
        val = (T)o;
    }
    return val;
}

This is the helper function I am using while reading data from the data reader.

Upvotes: 1

Views: 1025

Answers (1)

Shalem
Shalem

Reputation: 1516

Since you declared datetime as nullable, instead of converting it to datetime,convert it by using datetime?

Thus it allows datetime values with null values

Upvotes: 2

Related Questions