I'm trying to pull some data from my Database, but when it comes to Datetime I get an
InvalidCastException
This is my Database (SQL SERVER)
Create table Timeline(
start date,
end date
)
table Timeline
start(date) end(date)
03/07/2020 NULL
NULL 10/07/2020
15/07/2020 25/07/2020
This is my query
SELECT Timeline.start, Timeline.end FROM Timeline
This is my c# code
public class Timeline
{
public DateTime? startDate;
public DateTime? endDate;
public static Timeline fromRow(DataRow r)
{
Timeline a = new Timeline();
a.startDate = r.Field<DateTime?>("start");
a.endDate = r.Field<DateTime?>("end");
return a;
}
}
When I pull a NULL value it works, but when I pull a real date like 03/07/2020 It gives me InvalidCastException. Is there a way to check the value before putting it into the variable?