Monday, May 14, 2012

String was not recognized as a valid DateTime

I know such questions are in ton in SO but my situation seems little weird to me.



I have a textbox with a calendar extender control on my aspx page



Default format is "d" in extenders date format property.
When I choose my date say 15th May 2012 from the calendar,it gives me 5/15/2012, which is fine.



Since its a string and my db field is oftype datetime, so I am using



Convert.ToDateTime(TextBox.Text);   // TextBox.Text = 5/15/2012


But it throws the exception,
string was not recognized as valid datetime.



I then Change the code and used DateTime.Parse() but the issue remains. Then i tried to reformat the date something like this,



Convert.ToDateTime(string.Format("0:MM-dd-yyyy",TextBox.Text)).Date



but still its throwing exceptions..



Please help me.





No comments:

Post a Comment