Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm getting an error while debugging stating "FormaException was unhandled". Additional information: String was not recognized as a valid DateTime.

Below is my code:

string iString = df.cmbColumns.SelectedIndex.ToString();
DateTime oDate = DateTime.ParseExact(iString, "{0:dd/MM/yyyy hh:mm:tt}", null);
MessageBox.Show(oDate.ToString());
Posted
Comments
ZurdoDev 22-Jan-16 7:54am    
This means that iString is not a valid date time.
amagitech 22-Jan-16 7:57am    
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
I think df.cmbColumns.SelectedIndex.ToString() returnin integer format as 1,2,3,4.First error is that.
Second don't use null. Try CulturelInfo.InvariantCuluture.
Member 8010354 22-Jan-16 8:05am    
After using CulturelInfo.InvariantCuluture also, the issue is same.

Your format string is wrong, try this:
C#
DateTime oDate = DateTime.ParseExact(iString, "dd/MM/yyyy hh:mm:tt", null);

{0:dd/MM/yyyy hh:mm:tt} is used in String.Format to denote which parameter how to format.
 
Share this answer
 
Comments
Member 8010354 22-Jan-16 8:03am    
Even though, i'm getting the same issue.
To add to what Tomas says, not only is your format string wrong, but using DateTime.ParseExact on a single integer value as a string isn't going to work either.
And since SelectedIndex will only ever return an integer value, the string you are trying to convert will never be a "valid date".
I would suggest that if you want date values in a combobox, you load it with DateTime values and use the SelectedValue property instead.
 
Share this answer
 
Comments
Member 8010354 22-Jan-16 8:09am    
Actually, on a button click another form opens. Where i will select the DateTime columns. After selecting the column and click on OK, the date format should change in the datagridview in form1. So i used SelectedIndex.
Though i use selected value, the result is same.
OriginalGriff 22-Jan-16 8:17am    
Did you by any chance convert the SelectedValue to a string and then try to parse it? If so, why? If it's a DateTime, it's already a DateTime. If it isn't, then you need to look at exactly what it is - and it's probably not a date at all!
Tomas Takac 22-Jan-16 8:14am    
+5, SelectedValue is definitelly the way to go. I missed the SelectedIndex completely.
This question is asked multiple times a day, and like everyone else that asks this question you haven't told us the most important part of information....what is in iString. Because what is in iString isn't in the format that your ParseExact is asking for. That's exactly what the error message is telling you.

The only thing I can think of is that you don't know what the date format strings are. "tt", for example, is "AM" or "PM" so you are expecting a date like

22/01/2016 01:26:PM

It's unlikely your date is actually in this format, maybe you meant "ss" instead of "tt".

Custom Date and Time Format Strings[^]

Unless you post what is in iString there really is nothing anyone can tell you.
 
Share this answer
 
Comments
Member 8010354 22-Jan-16 9:15am    
DateTime.ParseExact() in C#

ParseExact method will allow you to specify the exact format of your date string to use for parsing. It is good to use this if your string is always in the same format. The format of the string representation must match the specified format exactly.


string iString = "2005-05-05 22:12 PM";
DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);
MessageBox.Show(oDate.ToString());


I saw it in google and used that format.
F-ES Sitecore 22-Jan-16 9:22am    
The format has to EXACTLY match what you're passing in (even down to 01 rather than 1 if you use dd). That example has " tt", your code has ":tt" so the example expects " PM", your code expects ":PM"

And, once more, you have not said exactly what is in iString so no-one can help you beyond what we've already said.
Member 8010354 25-Jan-16 2:27am    
Hi, sorry for the delay response. It's just a variable i have declared and i'm getting the problem especially when we are having 12 and 13 in the date.
(Ex: 12/13/2015). It's unable to figure out the month and date.
F-ES Sitecore 25-Jan-16 4:27am    
That is probably a localisation issue. 12/13 is a valid American format but not a valid British one. The "null" parameter you are passing to ParseExact is intended to be the culture you want the date to be valid for. You can use

System.Globalization.CultureInfo.CurrentCulture

for this param if the configured culture of the server matches the format you want, or you can hard-code it to a specific culture

System.Globalization.CultureInfo.GetCultureInfo("en-GB")
Member 8010354 25-Jan-16 4:52am    
Hi, I have used System.Globalization.CultureInfo.CurrentCulture but the result is still same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900