String was not recognized as a valid DateTime

1. Ever encountered the mentioned error and couldn’t get it solved.

2. Tried to get the data in a textbox and then the error kept bugging you.

3. c# send datetime to sql from textbox

4. edit the date format C#

well here is the solution to the problem:

DateTime dateValue;
string dateString = FromTimeTextBox.Text;
string[] formats = {“M/d/yyyy h:mm:ss tt”, “M/d/yyyy h:mm tt”,
“MM/dd/yyyy hh:mm:ss”, “M/d/yyyy h:mm:ss”,
“M/d/yyyy hh:mm tt”, “M/d/yyyy hh tt”,
“M/d/yyyy h:mm”, “M/d/yyyy h:mm”,
“MM/dd/yyyy hh:mm”, “M/dd/yyyy hh:mm”};

if (DateTime.TryParseExact(dateString, formats, new CultureInfo(“en-US”), DateTimeStyles.None, out dateValue))
timeslotDX.FromTime = dateValue;


and the above code will take any of the correct user entries.

for further references you can browse


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s