What’s wrong with this code?
Client c = new Client();
string format = "yyyy/MM/dd HH:mm:ss";
string dateAdded = now.ToString(format);
c.RegistrationDate = DateTime.Parse(dateAdded);
c.RegistrationDate is a dateTime object in the client class and I want it to insert to my database.
However It doesn’t convert the freaking date to the format in my mysql database. It always says that string format is incorrect. WHAT have i done wrong???? should I convert my Registration Date to string??? Thanks
**EDIT: Sorry I’ve forgot to mention. “now” is now = DateTime.Now; it gets the current time of the date and time.
A
DateTimedoesnt have a format – it’s just the date/time. (Whether it’s local time, UTC or whatever is a different matter, mind you.)Firstly, you shouldn’t be converting to and from text like you are: that’s just a recipe for trouble. Just use:
… performing any rounding you need to.
You haven’t shown how you’re trying to insert the value into your database. If you’re including the value in the SQL statement directly, that would explain it. You should be using a parameterized SQL statement and passing the value directly in the parameter – no conversion necessary.
If you’re already doing that, please show us the code you’re trying to use to insert the data, and we’ll see what we can do. See the documentation for some examples.