I have a bit of code that checks a table called upload tracker the issue I am having is the if the value of the datetime stored in the db is null it causing this section of code to crash.
using (var db = new SOMEntities1())
{
var uploadTracekerDate = db.UploadTrackers.Where(w => w.TableName == "Stock").FirstOrDefault();
if (uploadTracekerDate != null)
{
string lastUpLoadedDateConvetedTOSage =Convert.ToDateTime(uploadTracekerDate.LastImportedDate.Value.ToString("yyyy-MM-dd");
var stockwhereClause = string.Format(" and STOCK.RECORD_CREATE_DATE >= '{0}'", lastUpLoadedDateConvetedTOSage);
stockwhereClause = stockwhereClause + string.Format(" and STOCK.RECORD_MODIFY_DATE >= '{0}'",
lastUpLoadedDateConvetedTOSage);
sql += stockwhereClause;
}
}
What is the best way of handling this errror as the value seems to trigger a null exception no matter what I use to trap the object being null