Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error:
JavaScript
Incorrect datetime value: '4/10/2016 5:50:48 AM' for column 'LastLogin' at row 1


Query:
C#
string updateLastLogin = "Update mydb.user SET LastLogin='" + System.DateTime.Now + "' where Id='" + Session["UserId"].ToString() + "';";



is it that 4/10/2016 5:50:48 AM this value is not accepted by DATETIME datatype in mysql?

if yes then what should I do to store this value.
please help.

What I have tried:

Error:
JavaScript
Incorrect datetime value: '4/10/2016 5:50:48 AM' for column 'LastLogin' at row 1


Query:
C#
string updateLastLogin = "Update mydb.user SET LastLogin='" + System.DateTime.Now + "' where Id='" + Session["UserId"].ToString() + "';";



is it that 4/10/2016 5:50:48 AM this value is not accepted by DATETIME datatype in mysql?

if yes then what should I do to store this value.
please help.
Posted
Updated 9-Apr-16 19:29pm

Simple: don't do that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Passing the DateTime value as a DateTime in your C# code, (and the ID as whatever it should be, probably integer) will get rid of the problem, and will help to preserve your database - as well as making your code more readable.
Then go through the rest of your code and make sure you do the same everywhere else as well!
 
Share this answer
 
You need to convert it to mysql datetime format MySQL :: MySQL 5.7 Reference Manual :: 11.3.1 The DATE, DATETIME, and TIMESTAMP Types[^]
try this:
C#
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 
 
Share this answer
 
To expand on Griff's answer (if I may be so bold) I do a lot of international data integration work. Please, for the sanity of everyone, NEVER USE STRINGS FOR DATES.

Half my mortgage has been paid by that, alone.

Wait... What am I saying? Keep me in business! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900