Datetime object value for null but value still throwing null












-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










share|improve this question




















  • 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

    – 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
















-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










share|improve this question




















  • 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

    – 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














-1












-1








-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










share|improve this question
















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







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 14:49







david

















asked Nov 20 '18 at 14:37









daviddavid

114




114








  • 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

    – 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














  • 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

    – 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








3




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





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




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





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

– Steve
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

– Steve
Nov 20 '18 at 14:40




1




1





@Fildor it was there just not indented correctly now fixed

– david
Nov 20 '18 at 14:49





@Fildor it was there just not indented correctly now fixed

– david
Nov 20 '18 at 14:49












2 Answers
2






active

oldest

votes


















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.






share|improve this answer



















  • 1





    your on the money there marc very helpfull twice in a row sir.

    – david
    Nov 20 '18 at 14:47











  • @david well, if either has answered your question - consider pressing the green tick button!

    – Marc Gravell
    Nov 20 '18 at 14:53



















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.






share|improve this answer
























  • This is not a scenario where DBNull applies - this sounds very much like an ORM scenario with DateTime?

    – Marc Gravell
    Nov 20 '18 at 14:52











  • IAlso its not right to create a question when you do not have the rep to comment on a question.

    – david
    Nov 20 '18 at 14:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395378%2fdatetime-object-value-for-null-but-value-still-throwing-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer



















  • 1





    your on the money there marc very helpfull twice in a row sir.

    – david
    Nov 20 '18 at 14:47











  • @david well, if either has answered your question - consider pressing the green tick button!

    – Marc Gravell
    Nov 20 '18 at 14:53
















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.






share|improve this answer



















  • 1





    your on the money there marc very helpfull twice in a row sir.

    – david
    Nov 20 '18 at 14:47











  • @david well, if either has answered your question - consider pressing the green tick button!

    – Marc Gravell
    Nov 20 '18 at 14:53














1












1








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 14:45









Marc GravellMarc Gravell

781k19421382544




781k19421382544








  • 1





    your on the money there marc very helpfull twice in a row sir.

    – david
    Nov 20 '18 at 14:47











  • @david well, if either has answered your question - consider pressing the green tick button!

    – Marc Gravell
    Nov 20 '18 at 14:53














  • 1





    your on the money there marc very helpfull twice in a row sir.

    – david
    Nov 20 '18 at 14:47











  • @david well, if either has answered your question - consider pressing the green tick button!

    – Marc Gravell
    Nov 20 '18 at 14:53








1




1





your on the money there marc very helpfull twice in a row sir.

– david
Nov 20 '18 at 14:47





your on the money there marc very helpfull twice in a row sir.

– david
Nov 20 '18 at 14:47













@david well, if either has answered your question - consider pressing the green tick button!

– Marc Gravell
Nov 20 '18 at 14:53





@david well, if either has answered your question - consider pressing the green tick button!

– Marc Gravell
Nov 20 '18 at 14:53













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.






share|improve this answer
























  • This is not a scenario where DBNull applies - this sounds very much like an ORM scenario with DateTime?

    – Marc Gravell
    Nov 20 '18 at 14:52











  • IAlso its not right to create a question when you do not have the rep to comment on a question.

    – david
    Nov 20 '18 at 14:57
















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.






share|improve this answer
























  • This is not a scenario where DBNull applies - this sounds very much like an ORM scenario with DateTime?

    – Marc Gravell
    Nov 20 '18 at 14:52











  • IAlso its not right to create a question when you do not have the rep to comment on a question.

    – david
    Nov 20 '18 at 14:57














0












0








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 14:51









XYZXYZ

163




163













  • This is not a scenario where DBNull applies - this sounds very much like an ORM scenario with DateTime?

    – Marc Gravell
    Nov 20 '18 at 14:52











  • IAlso its not right to create a question when you do not have the rep to comment on a question.

    – david
    Nov 20 '18 at 14:57



















  • This is not a scenario where DBNull applies - this sounds very much like an ORM scenario with DateTime?

    – Marc Gravell
    Nov 20 '18 at 14:52











  • IAlso its not right to create a question when you do not have the rep to comment on a question.

    – david
    Nov 20 '18 at 14:57

















This is not a scenario where DBNull applies - this sounds very much like an ORM scenario with DateTime?

– Marc Gravell
Nov 20 '18 at 14:52





This is not a scenario where DBNull applies - this sounds very much like an ORM scenario with DateTime?

– Marc Gravell
Nov 20 '18 at 14:52













IAlso its not right to create a question when you do not have the rep to comment on a question.

– david
Nov 20 '18 at 14:57





IAlso its not right to create a question when you do not have the rep to comment on a question.

– david
Nov 20 '18 at 14:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395378%2fdatetime-object-value-for-null-but-value-still-throwing-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith