Wednesday, March 28, 2012

String was not recognized as a valid DateTime.

I am capturing a value from a text field and insert it into database of data type DateTime. I have used Convert.ToDateTime("stringdate") to convert it into datetime but i get the above error. But when I converted DateTime value to string and then converted it into DateTime using above method it works fine. I don't know what I need to do.

thanx for any helpIt's likely because the DateTime class can't figure out what date format the string is in. If you use DateTime.Parse that might work or if you always know what format your string is in use DateTime.ParseExact, because with that you can specify the date format.
Try:

FromDate = Convert.ToDateTime(Request.Form["FromDate"].ToString()).Date;

Where FromDate is the txtField - I had trouble with this - Seems like going around the houses a bit to me, but it works.

Pete

0 comments:

Post a Comment