-1

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

david
  • 21
  • 4
  • 3
    which bit is triggering a null exception? also: I hate to say it, but: concatenating values to create SQL is just ... doomed to all sorts of failure; ideally, this should be parameterized – Marc Gravell Nov 20 '18 at 14:38
  • 1
    it looks to me like the easiest way to cause this would be for `LastImportedDate` to be `null`; what do you want to do in that case? maybe just don't add the extra filter? – Marc Gravell Nov 20 '18 at 14:40
  • And what are you doing here to prevent a null reference exception? Did you read [What is a NullReferenceException and how do I fix it](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Steve Nov 20 '18 at 14:40
  • 1
    @Fildor it was there just not indented correctly now fixed – david Nov 20 '18 at 14:49

2 Answers2

1

Given the code shown, the most likely scenario is that uploadTracekerDate is non-null, and is an instance that has LastImportedDate as an empty (null) Nullable<DateTime> (DateTime?).

This means that it is .Value that is throwing the exception, so you need to think about what you actually want to do in the scenario when LastImportedDate is null, and implement that - probably by checking if (uploadTracekerDate.LastImportedDate.HasValue) before accessing the .Value.

However! And this is important: please please move away from the approach of concatenating strings with values to create SQL - it is a terrible idea for multiple reasons; in this case, the most likely problem will be i18n with locale problems, but the same approach can also cause serious SQL injection problems. Whenever possible (which is almost always): prefer SQL parameters.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

First of all, your code seems to be wrong.

string lastUpLoadedDateConvetedTOSage =Convert.ToDateTime(uploadTracekerDate.LastImportedDate.Value.ToString("yyyy-MM-dd");

... is missing a closing bracket.

Secondly, try comparing to DBNull.Value instead, when using values from an SQL database.

XYZ
  • 39
  • 4