0

I'm using c sharp and mysql. when i'm trying to insert a date from DateTime type to date column in DB all i get is 0000-00-00

i'm parsing my DateTime like this : sessionDate.ToShortDateString()

but getting empty date ...

any help please

8
  • is the DB column Date or DateTime type? Commented Aug 19, 2011 at 15:30
  • What DB are you using and how are you entering the DateTime into the DB, show some more specific code. Commented Aug 19, 2011 at 15:31
  • You need to post more of your code on how you do the insert, but without any context it does look odd to convert a datetime to a string when the targetcolumn is date Commented Aug 19, 2011 at 15:32
  • the column is in a type of "Date" the insert is by "insert to DB query" where i'm sending a string of sessionDate.ToShortDateString() to the date column Commented Aug 19, 2011 at 15:33
  • eddy - if i'm not parsing my date in c# then i'll send full date time to DB , aka "8/19/2011 12:00:00 AM" by parsing it i send "8/19/2011" instead, but mySql Date expecting "2001-08-19"... i also tried to "tostring it" by using "YYYY-MM-DD" format, but then i got an exception from DB Commented Aug 19, 2011 at 15:38

2 Answers 2

1

MySql uses date in yyyy-MM-dd format,So U can overide the toString() method Of DateTime.Now property.

YourDate= DateTime.Now.ToString("yyyy-MM-dd");
Sign up to request clarification or add additional context in comments.

2 Comments

it doesnt work, i get an exception : "Incorrect date value: '1984' for column 'sessionDate' at row 1" and i've no idea why 1984 is there... the date i'm insert is current date...
Can You Post the Entire Code.. Will look at it. did u tried using BreakPoints to Explore the values???
0

Try this to format your date time to a string value that is acceptable to MySQL

String.Format("{0:yyyy-MM-dd}", sessionDate);

2 Comments

didnt work i'm getting next exception from DB : Incorrect date value: '1984' for column 'sessionDate' at row 1... i've no idea why 1984 is there... the date i'm insert is current date...
I think we'll probably need a larger snippet of your code. Its difficult to tell where the error is coming from by just looking at this little information. Can you give the table definition and also the entire block of code which you are using to try and insert the date in the database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.