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; }
}