Harvey
Harvey

Reputation: 389

Get value of date and store it in MySQL database?

I've got the error:

INPUT STRING WAS NOT IN A CORRECT FORMAT

when running the code below.

So what do you think here is the error? How will I format the date in the DateTimePicker to store properly in MySQL database?

Here is my code (I included only the relevant code which I think is the error):

cmd.Parameters.AddWithValue("@Rdate", _order.dateTimePicker_Requested.Value.ToString("yyyy-MM-dd"));
cmd.Parameters.AddWithValue("@Ndate", _order.dateTimePicker_Needed.Value.ToString("yyyy-MM-dd"));
cmd.Parameters.AddWithValue("@CodeDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd hh:mm:ss"));

Upvotes: 1

Views: 260

Answers (3)

Kevin Shah
Kevin Shah

Reputation: 1617

If your variable represents datetime value then its no need to convert it to string it automatically mapped with MySQL column type and if it still not work then reply. I will post with some code that will convert datetime to proper so that it would mapped with column.

Thank you.

Upvotes: 0

Obama
Obama

Reputation: 2612

You should set :

string.Format("{0:yyyy-MM-dd}", your_date);

Hope this helps!

Upvotes: 1

SLaks
SLaks

Reputation: 887245

Unless your MySQL columns are strings (which they shouldn't be), you should be passing actual dates as parameters.

Don't call .ToString().

Upvotes: 1

Related Questions