How to deal with Object can not be cast from DBNull to other types?

235 Views Asked by At

I have checked couple of post here suggesting how to deal with DBNull vale so I followed the same. Here are my code. But still I am getting the error message

Object can not be cast from DBNull to other types.

What am I missing here?

     public IEnumerable<MyClass> ConvertRowToStd(Datatable table, Datetime dt)
     {
       List<MyClass> targets = new List<MyClass>();
       EntityMapper mapper =  new EntityMapper();

       return(
               from DataRow r in table.Rows
               select new MyClass
               {
                   StartDate = dt.Date,
                   // other mapping goes here
                   EndDate =  row["End_Date"] != DBBull.Value ? Convert.ToDatetime(row["End_Date"]) : (DateTime?)null
                }
             )
       }

Here is MyClass

public class MyClass
{
    public DateTime StartDate {get; set;}
    //
    //
    public DateTime? EndDate {get; set;}

}

0

There are 0 best solutions below