简体   繁体   中英

Handling DBNull values for DateTime

I'm converting my DataTable to generic List and as I'm converting row value to object for the list, it's giving Cast Exception. I've tried handling the DBNull using the code

if (dataRow["DateCompleted"] == DBNull.Value)
{
    dataRow["DateCompleted"] = null;
}
if (dataRow["TotalRecords"] == DBNull.Value)
{
    dataRow["TotalRecords"] = 0;
}
if (dataRow["CompanyName"] == DBNull.Value)
{
    dataRow["CompanyName"] = "";
}

but DateCompleted is not accepting the null, DBNull or empty string.

After the process, I'm making object like

DemoData demoValue = new DemoData
{
    CompanyName = dataRow["CompanyName"].ToString(),
    DateCompleted = (DateTime)dataRow["DateCompleted"],
    TotalRecords = (int)dataRow["TotalRecords"]
};

and adding the object to the list

DataList.Add(demoValue);

My list is

public List<DemoData> DataList = new List<DemoData>();

and my class is

public class DemoData
{
    public string CompanyName { get; set; }
    public DateTime DateCompleted { get; set; }
    public int TotalRecords { get; set; }
}

The answer depends on business logic you want to perform if DemoData is not completed:

You may want to insist on null ; it makes, however, the logic more complex (since null is a special case now), and requires DemoData modification:

public class DemoData
{
     public string CompanyName { get; set; }
     // DateTime? - DateCompleted is Nullable<DateTime>   
     public DateTime? DateCompleted { get; set; }
     public int TotalRecords { get; set; }
}
...

DemoData demoValue = new DemoData {
  CompanyName = dataRow["CompanyName"].ToString(),
  DateCompleted = dataRow.IsDbNull("DateCompleted") 
    ? null // <- now we can assign null to Nullable<T>   
    : (DateTime?) dataRow["DateCompleted"],
  TotalRecords = (int)dataRow["TotalRecords"]
};

You may want to adopt the following (typical) logic: "If DemoData has not completed yet, it shall be eventually completed in a (distant) future ":

...
if (dataRow["DateCompleted"] == DBNull.Value)
{
    // Well, at 31 Dec 9999 it'll be completed for sure...
    dataRow["DateCompleted"] = DateTime.MaxValue;
}

...

DemoData demoValue = new DemoData
{
  CompanyName = dataRow["CompanyName"].ToString(),
  DateCompleted = (DateTime)dataRow["DateCompleted"],
  TotalRecords = (int)dataRow["TotalRecords"]
};

So I figured out that the problem was I was assigning null values to the dataRow itself so it was not accepting any values, I wrote the code like this and now it's working fine

DateTime? dateCompleted;
if (dataRow["DateCompleted"] == DBNull.Value)
{
    dateCompleted= null;
}
else
{
    dateCompleted= (DateTime)dataRow["DateCompleted"];
}
DemoData demoValue = new DemoData
{
    CompanyName = dataRow["CompanyName"].ToString(),
    DateCompleted = dateCompleted,
    TotalRecords = (int)dataRow["TotalRecords"]
};

and for the class

public class DemoData
{
     public string CompanyName { get; set; }
     public DateTime? DateCompleted { get; set; }
     public int TotalRecords { get; set; }
}

Make DateCompleted nullable in your class:

public class DemoData
{
    public string CompanyName { get; set; }
    public DateTime? DateCompleted { get; set; }
    public int TotalRecords { get; set; }
}

Since your source aa datatable try something like this

            DataTable dt = new DataTable();
            dt.Columns.Add("date", typeof(DateTime));
            dt.Columns["date"].AllowDBNull = true;

            dt.Rows.Add();
            dt.Rows[0]["date"] = DBNull.Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM