Datetime object value for null but value still throwing null
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#
add a comment |
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#
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 forLastImportedDateto benull; 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
add a comment |
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#
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#
c#
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 forLastImportedDateto benull; 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
add a comment |
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 forLastImportedDateto benull; 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
add a comment |
2 Answers
2
active
oldest
votes
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.
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
add a comment |
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.
This is not a scenario whereDBNullapplies - this sounds very much like an ORM scenario withDateTime?
– 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Nov 20 '18 at 14:45
Marc Gravell♦Marc 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
add a comment |
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
add a comment |
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.
This is not a scenario whereDBNullapplies - this sounds very much like an ORM scenario withDateTime?
– 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
add a comment |
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.
This is not a scenario whereDBNullapplies - this sounds very much like an ORM scenario withDateTime?
– 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
add a comment |
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.
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.
answered Nov 20 '18 at 14:51
XYZXYZ
163
163
This is not a scenario whereDBNullapplies - this sounds very much like an ORM scenario withDateTime?
– 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
add a comment |
This is not a scenario whereDBNullapplies - this sounds very much like an ORM scenario withDateTime?
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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

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
LastImportedDateto benull; 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