String was not recognized as a valid datetime, source date is 0000-00-00 00:00:000
UPDATE
Apparently it was not caused by the 0000-00-00 00:00:000
, the program error out when the value was 2016-04-21 00:00:00.000
Any idea what could be the cause?
I have a VS C# program that will SELECT from MSSQL then INSERT/ON DUPLICATE UPDATE into MySQL database. I have a particular row which the datetime is NULL
, my MSSQL query and result are:
Query
SELECT UserID,LastPasswordDate,
CASE WHEN LastPasswordDate IS NULL THEN '0000-00-00 00:00:00:000'
ELSE convert(varchar, LastPasswordDate, 121) END as LastPasswordDate2 from users
order by LastPasswordDate
Result
C# code
string LastPasswordDate = row["LastPasswordDate"].ToString(); // Or
//DateTime LastPasswordDate = DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null);
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", LastPasswordDate);
insertUserCommand.ExecuteNonQuery();
insertUserCommand.Parameters.Clear();
tran.Commit();
I tried to use C# conversion but keep getting same error message as per title mentioned
c# mysql
add a comment |
UPDATE
Apparently it was not caused by the 0000-00-00 00:00:000
, the program error out when the value was 2016-04-21 00:00:00.000
Any idea what could be the cause?
I have a VS C# program that will SELECT from MSSQL then INSERT/ON DUPLICATE UPDATE into MySQL database. I have a particular row which the datetime is NULL
, my MSSQL query and result are:
Query
SELECT UserID,LastPasswordDate,
CASE WHEN LastPasswordDate IS NULL THEN '0000-00-00 00:00:00:000'
ELSE convert(varchar, LastPasswordDate, 121) END as LastPasswordDate2 from users
order by LastPasswordDate
Result
C# code
string LastPasswordDate = row["LastPasswordDate"].ToString(); // Or
//DateTime LastPasswordDate = DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null);
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", LastPasswordDate);
insertUserCommand.ExecuteNonQuery();
insertUserCommand.Parameters.Clear();
tran.Commit();
I tried to use C# conversion but keep getting same error message as per title mentioned
c# mysql
Once you have the data in a datareader/datatable the source you retrieved it from does not matter.
– bradbury9
Nov 13 '18 at 8:15
Just read it into a nullable C# DateTime... No need to pass by a string.... And then use the null internally, rather than that nasty "zero" date...
– Mark
Nov 13 '18 at 8:20
When you check for null you should not return an invalid date. You should return either a valid date ornull
.
– bradbury9
Nov 13 '18 at 8:21
Why can't you store NULL as NULL? If you really must then transfer the dates as strings.
– Salman A
Nov 13 '18 at 8:24
add a comment |
UPDATE
Apparently it was not caused by the 0000-00-00 00:00:000
, the program error out when the value was 2016-04-21 00:00:00.000
Any idea what could be the cause?
I have a VS C# program that will SELECT from MSSQL then INSERT/ON DUPLICATE UPDATE into MySQL database. I have a particular row which the datetime is NULL
, my MSSQL query and result are:
Query
SELECT UserID,LastPasswordDate,
CASE WHEN LastPasswordDate IS NULL THEN '0000-00-00 00:00:00:000'
ELSE convert(varchar, LastPasswordDate, 121) END as LastPasswordDate2 from users
order by LastPasswordDate
Result
C# code
string LastPasswordDate = row["LastPasswordDate"].ToString(); // Or
//DateTime LastPasswordDate = DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null);
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", LastPasswordDate);
insertUserCommand.ExecuteNonQuery();
insertUserCommand.Parameters.Clear();
tran.Commit();
I tried to use C# conversion but keep getting same error message as per title mentioned
c# mysql
UPDATE
Apparently it was not caused by the 0000-00-00 00:00:000
, the program error out when the value was 2016-04-21 00:00:00.000
Any idea what could be the cause?
I have a VS C# program that will SELECT from MSSQL then INSERT/ON DUPLICATE UPDATE into MySQL database. I have a particular row which the datetime is NULL
, my MSSQL query and result are:
Query
SELECT UserID,LastPasswordDate,
CASE WHEN LastPasswordDate IS NULL THEN '0000-00-00 00:00:00:000'
ELSE convert(varchar, LastPasswordDate, 121) END as LastPasswordDate2 from users
order by LastPasswordDate
Result
C# code
string LastPasswordDate = row["LastPasswordDate"].ToString(); // Or
//DateTime LastPasswordDate = DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null);
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", LastPasswordDate);
insertUserCommand.ExecuteNonQuery();
insertUserCommand.Parameters.Clear();
tran.Commit();
I tried to use C# conversion but keep getting same error message as per title mentioned
c# mysql
c# mysql
edited Nov 13 '18 at 9:53
4 Leave Cover
asked Nov 13 '18 at 8:01
4 Leave Cover4 Leave Cover
86992650
86992650
Once you have the data in a datareader/datatable the source you retrieved it from does not matter.
– bradbury9
Nov 13 '18 at 8:15
Just read it into a nullable C# DateTime... No need to pass by a string.... And then use the null internally, rather than that nasty "zero" date...
– Mark
Nov 13 '18 at 8:20
When you check for null you should not return an invalid date. You should return either a valid date ornull
.
– bradbury9
Nov 13 '18 at 8:21
Why can't you store NULL as NULL? If you really must then transfer the dates as strings.
– Salman A
Nov 13 '18 at 8:24
add a comment |
Once you have the data in a datareader/datatable the source you retrieved it from does not matter.
– bradbury9
Nov 13 '18 at 8:15
Just read it into a nullable C# DateTime... No need to pass by a string.... And then use the null internally, rather than that nasty "zero" date...
– Mark
Nov 13 '18 at 8:20
When you check for null you should not return an invalid date. You should return either a valid date ornull
.
– bradbury9
Nov 13 '18 at 8:21
Why can't you store NULL as NULL? If you really must then transfer the dates as strings.
– Salman A
Nov 13 '18 at 8:24
Once you have the data in a datareader/datatable the source you retrieved it from does not matter.
– bradbury9
Nov 13 '18 at 8:15
Once you have the data in a datareader/datatable the source you retrieved it from does not matter.
– bradbury9
Nov 13 '18 at 8:15
Just read it into a nullable C# DateTime... No need to pass by a string.... And then use the null internally, rather than that nasty "zero" date...
– Mark
Nov 13 '18 at 8:20
Just read it into a nullable C# DateTime... No need to pass by a string.... And then use the null internally, rather than that nasty "zero" date...
– Mark
Nov 13 '18 at 8:20
When you check for null you should not return an invalid date. You should return either a valid date or
null
.– bradbury9
Nov 13 '18 at 8:21
When you check for null you should not return an invalid date. You should return either a valid date or
null
.– bradbury9
Nov 13 '18 at 8:21
Why can't you store NULL as NULL? If you really must then transfer the dates as strings.
– Salman A
Nov 13 '18 at 8:24
Why can't you store NULL as NULL? If you really must then transfer the dates as strings.
– Salman A
Nov 13 '18 at 8:24
add a comment |
3 Answers
3
active
oldest
votes
The first thing you should know is that datetime
data type in MySQL has minimum value of 1000-01-01 00:00:00.000
, not 0000-00-00 00:00:00.000
which used as "zero" value display when using datetime conversion for invalid date. Second, the DateTime.MinValue
has minimum value of 0001-01-01 00:00:00.000
, not suitable for conversion against MySQL's "zero" value as mentioned before.
If the target column in MySQL DB has nullable datetime
data type, you should use TryParseExact()
and use DBNull.Value
for assign null value when the "zero" date cannot be parsed:
DateTime date;
DateTime? LastPasswordDate;
if (DateTime.TryParseExact(row["LastPasswordDate"].ToString(), out date))
LastPasswordDate = date;
else
LastPasswordDate = null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
But in my thoughts it's better to return null value from T-SQL query and check it with Convert.IsDBNull()
, then use DBNull.Value
for assign null values into database column:
DateTime? LastPasswordDate = !Convert.IsDBNull(row["LastPasswordDate"]) ? DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null) : null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
add a comment |
the problem is your string '0000-00-00 00:00:00:000' that you are using for the check cannot be converted to a valid date time in code even though it can be saved like that in the database. best solution is to set a default value of null in the database and look for that
add a comment |
In .Net, DateTime.MinValue is 1/1/0001 12:00:00 AM, so 0000-00-00 00:00:00 is outside of the range of valid DateTime values.
see DateTime.MinValue
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%2f53276347%2fstring-was-not-recognized-as-a-valid-datetime-source-date-is-0000-00-00-00000%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The first thing you should know is that datetime
data type in MySQL has minimum value of 1000-01-01 00:00:00.000
, not 0000-00-00 00:00:00.000
which used as "zero" value display when using datetime conversion for invalid date. Second, the DateTime.MinValue
has minimum value of 0001-01-01 00:00:00.000
, not suitable for conversion against MySQL's "zero" value as mentioned before.
If the target column in MySQL DB has nullable datetime
data type, you should use TryParseExact()
and use DBNull.Value
for assign null value when the "zero" date cannot be parsed:
DateTime date;
DateTime? LastPasswordDate;
if (DateTime.TryParseExact(row["LastPasswordDate"].ToString(), out date))
LastPasswordDate = date;
else
LastPasswordDate = null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
But in my thoughts it's better to return null value from T-SQL query and check it with Convert.IsDBNull()
, then use DBNull.Value
for assign null values into database column:
DateTime? LastPasswordDate = !Convert.IsDBNull(row["LastPasswordDate"]) ? DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null) : null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
add a comment |
The first thing you should know is that datetime
data type in MySQL has minimum value of 1000-01-01 00:00:00.000
, not 0000-00-00 00:00:00.000
which used as "zero" value display when using datetime conversion for invalid date. Second, the DateTime.MinValue
has minimum value of 0001-01-01 00:00:00.000
, not suitable for conversion against MySQL's "zero" value as mentioned before.
If the target column in MySQL DB has nullable datetime
data type, you should use TryParseExact()
and use DBNull.Value
for assign null value when the "zero" date cannot be parsed:
DateTime date;
DateTime? LastPasswordDate;
if (DateTime.TryParseExact(row["LastPasswordDate"].ToString(), out date))
LastPasswordDate = date;
else
LastPasswordDate = null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
But in my thoughts it's better to return null value from T-SQL query and check it with Convert.IsDBNull()
, then use DBNull.Value
for assign null values into database column:
DateTime? LastPasswordDate = !Convert.IsDBNull(row["LastPasswordDate"]) ? DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null) : null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
add a comment |
The first thing you should know is that datetime
data type in MySQL has minimum value of 1000-01-01 00:00:00.000
, not 0000-00-00 00:00:00.000
which used as "zero" value display when using datetime conversion for invalid date. Second, the DateTime.MinValue
has minimum value of 0001-01-01 00:00:00.000
, not suitable for conversion against MySQL's "zero" value as mentioned before.
If the target column in MySQL DB has nullable datetime
data type, you should use TryParseExact()
and use DBNull.Value
for assign null value when the "zero" date cannot be parsed:
DateTime date;
DateTime? LastPasswordDate;
if (DateTime.TryParseExact(row["LastPasswordDate"].ToString(), out date))
LastPasswordDate = date;
else
LastPasswordDate = null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
But in my thoughts it's better to return null value from T-SQL query and check it with Convert.IsDBNull()
, then use DBNull.Value
for assign null values into database column:
DateTime? LastPasswordDate = !Convert.IsDBNull(row["LastPasswordDate"]) ? DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null) : null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
The first thing you should know is that datetime
data type in MySQL has minimum value of 1000-01-01 00:00:00.000
, not 0000-00-00 00:00:00.000
which used as "zero" value display when using datetime conversion for invalid date. Second, the DateTime.MinValue
has minimum value of 0001-01-01 00:00:00.000
, not suitable for conversion against MySQL's "zero" value as mentioned before.
If the target column in MySQL DB has nullable datetime
data type, you should use TryParseExact()
and use DBNull.Value
for assign null value when the "zero" date cannot be parsed:
DateTime date;
DateTime? LastPasswordDate;
if (DateTime.TryParseExact(row["LastPasswordDate"].ToString(), out date))
LastPasswordDate = date;
else
LastPasswordDate = null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
But in my thoughts it's better to return null value from T-SQL query and check it with Convert.IsDBNull()
, then use DBNull.Value
for assign null values into database column:
DateTime? LastPasswordDate = !Convert.IsDBNull(row["LastPasswordDate"]) ? DateTime.ParseExact(row["LastPasswordDate"].ToString(), "yyyy-MM-dd HH:mm:ss:fff", null) : null;
insertUserCommand.Parameters.AddWithValue("@LastPasswordDate", (object)LastPasswordDate ?? DBNull.Value);
insertUserCommand.ExecuteNonQuery();
answered Nov 13 '18 at 9:26
Tetsuya YamamotoTetsuya Yamamoto
15.7k42240
15.7k42240
add a comment |
add a comment |
the problem is your string '0000-00-00 00:00:00:000' that you are using for the check cannot be converted to a valid date time in code even though it can be saved like that in the database. best solution is to set a default value of null in the database and look for that
add a comment |
the problem is your string '0000-00-00 00:00:00:000' that you are using for the check cannot be converted to a valid date time in code even though it can be saved like that in the database. best solution is to set a default value of null in the database and look for that
add a comment |
the problem is your string '0000-00-00 00:00:00:000' that you are using for the check cannot be converted to a valid date time in code even though it can be saved like that in the database. best solution is to set a default value of null in the database and look for that
the problem is your string '0000-00-00 00:00:00:000' that you are using for the check cannot be converted to a valid date time in code even though it can be saved like that in the database. best solution is to set a default value of null in the database and look for that
answered Nov 13 '18 at 8:06
LegoLego
409
409
add a comment |
add a comment |
In .Net, DateTime.MinValue is 1/1/0001 12:00:00 AM, so 0000-00-00 00:00:00 is outside of the range of valid DateTime values.
see DateTime.MinValue
add a comment |
In .Net, DateTime.MinValue is 1/1/0001 12:00:00 AM, so 0000-00-00 00:00:00 is outside of the range of valid DateTime values.
see DateTime.MinValue
add a comment |
In .Net, DateTime.MinValue is 1/1/0001 12:00:00 AM, so 0000-00-00 00:00:00 is outside of the range of valid DateTime values.
see DateTime.MinValue
In .Net, DateTime.MinValue is 1/1/0001 12:00:00 AM, so 0000-00-00 00:00:00 is outside of the range of valid DateTime values.
see DateTime.MinValue
answered Nov 13 '18 at 9:07
Stefan IllnerStefan Illner
165211
165211
add a comment |
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%2f53276347%2fstring-was-not-recognized-as-a-valid-datetime-source-date-is-0000-00-00-00000%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
Once you have the data in a datareader/datatable the source you retrieved it from does not matter.
– bradbury9
Nov 13 '18 at 8:15
Just read it into a nullable C# DateTime... No need to pass by a string.... And then use the null internally, rather than that nasty "zero" date...
– Mark
Nov 13 '18 at 8:20
When you check for null you should not return an invalid date. You should return either a valid date or
null
.– bradbury9
Nov 13 '18 at 8:21
Why can't you store NULL as NULL? If you really must then transfer the dates as strings.
– Salman A
Nov 13 '18 at 8:24