Link to home
Start Free TrialLog in
Avatar of zintech
zintech

asked on

Puzzling error "String was not recognized as a valid DateTime"

I have a Drop Down List with a list of values of dates such as 01/01/2011, 12/24/2011, etc. etc.  What I am trying to do is take the value that is selected in the Drop Down List such as 01/01/2011 and take this value and convert it to a DateTime type.  I always get the message "String was not recognized as a valid DateTime. "  My code is as follows:

DateTime currentdate = DateTime.ParseExact(DropDownList1.SelectedValue, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

1) Try setting the selectedValue to a string/date variable.

Then replace that variable in .ParseExact method in place of DropDownList1.SelectedValue

And then check it

OR

DateTime selDate = DateTime.ParseExact(DropDownList1.SelectedValue, "ddMMyyyy", System.Globalization.CultureInfo.CurrentCulture);  
string str = selDate.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
ASKER CERTIFIED SOLUTION
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial