G
Guest
I have a start and end date in my application. If a user does not know their
dates yet, I want them, they will be null in the DB and I want them to be
blank in the application. So, I'm trying to figure out how to use a nullable
date, and want to return it in my method.
I tried to cast the output parameter, but I get a run-time cast error:
return new TimeLine((DateTime?)command.Parameters["@startDate"].Value,
(DateTime?)command.Parameters["@endDate"].Value);
Then I changed to the code to what you see below, and it worked. But I am
wondering is there a better way to do this when working with nullable dates?
DateTime? startDate;
DateTime? endDate;
if (command.Parameters["@startDate"].Value is DBNull)
startDate = null;
else
startDate = (DateTime?)command.Parameters["@startDate"].Value;
if (command.Parameters["@endDate"].Value is DBNull)
endDate = null;
else
endDate = (DateTime?)command.Parameters["@endDate"].Value;
return new TimeLine(startDate, endDate);
dates yet, I want them, they will be null in the DB and I want them to be
blank in the application. So, I'm trying to figure out how to use a nullable
date, and want to return it in my method.
I tried to cast the output parameter, but I get a run-time cast error:
return new TimeLine((DateTime?)command.Parameters["@startDate"].Value,
(DateTime?)command.Parameters["@endDate"].Value);
Then I changed to the code to what you see below, and it worked. But I am
wondering is there a better way to do this when working with nullable dates?
DateTime? startDate;
DateTime? endDate;
if (command.Parameters["@startDate"].Value is DBNull)
startDate = null;
else
startDate = (DateTime?)command.Parameters["@startDate"].Value;
if (command.Parameters["@endDate"].Value is DBNull)
endDate = null;
else
endDate = (DateTime?)command.Parameters["@endDate"].Value;
return new TimeLine(startDate, endDate);