I try to check if the variables DateTime
"starttime"
and "endtime"
values have null
, and then try to initialize the value empty
as shown below, but I encountered below compilation errors. What is the best way to achieve this?
string htmllink = "";
DateTime? starttime = null;
DateTime? endtime = null;
htmllink = (dbNullCheck.isColumnNull(rdr, "html_link")) ? "" : rdr.GetString(3);
starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? "" : rdr.GetString(4);
endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? "" : rdr.GetString(5);
results.htmllist.Add(new gethtmllist() { resulthtmllink = htmllink,
duration = (starttime - endtime).ToString() });
Mistake:
Error 2: Cannot implicitly convert type 'string' to 'System.DateTime?'
UPDATE: -
string htmllink = "";
htmllink = (dbNullCheck.isColumnNull(rdr, "html_link")) ? "" : rdr.GetString(3);
DateTime? starttime = (dbNullCheck.isColumnNull(rdr, "start_time")) ? new DateTime() : rdr.GetDateTime(4);
DateTime? endtime = (dbNullCheck.isColumnNull(rdr, "end_time")) ? new DateTime() : rdr.GetDateTime(5);
results.htmllist.Add(new gethtmllist()
{
resulthtmllink = htmllink,
duration = starttime.HasValue && endtime.HasValue ? (endtime.Value - starttime.Value).ToString() : ""
});
source
share