I am inserting a date into my database, the value which comes from:
s.theDate = Convert.ToDateTime("06-13-2012");
and I get the error, "String was not recognized as a valid DateTime". How do I resolve this?
The reason is failed in other computers is likely to be they have different Cultures, one computer could be trying to parse in American format MM/dd/yyyy and the other one is in dd/MM/yyyy. You can use DateTime. ParseExact() and specify the format and culture you are trying to parse.
[FormatException: String was not recognized as a valid DateTime.] then it is telling you that whatever is in the text box cannot be recognised as a date. You need to either change the textbox contents so it is a valid date, or provide a custom date format for the date parser that matches the text box contents, or both.
Use the DateTime. TryParseExact method in C# for Date Format validation. They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.
Try this:
DateTime.ParseExact("06-13-2012", "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With