DateTime.Parse problem (IndexOutOfRange Exception)  
Author Message
Enrico Sicignano





PostPosted: .NET Base Class Library, DateTime.Parse problem (IndexOutOfRange Exception) Top

This is the first time in this forum, and my question is:

This code fragment is very simple

string s = DateTime.Now.ToString(Thread.CurrentThread.CurrentCulture);
DateTime.Parse(s, Thread.CurrentThread.CurrentCulture);

OR

DateTime.Parse("25/01/05 11.27.31", CultureInfo.CreateSpecificCulture("it-IT"));

OR

DateTime.Parse("dummy", CultureInfo.CreateSpecificCulture("it-IT"));

I expected a FormatException if the date is wrong, but in all cases the result is a IndexOutOfRangeException.

Where is the problem

The configuration of my PC is:

  1. Windows XP Professional Service Pack 2 (Italian);
  2. Visual Studio 2005 v. 8.0.50727.42 (Italian);
  3. .NET Framewok v. 2.0.50727 (Italian).

With .NET Framework 1.1 and Visual Studio 2003 this problem does not exist.

Thanks to all.




.NET Development4  
 
 
Stefan Prodan





PostPosted: .NET Base Class Library, DateTime.Parse problem (IndexOutOfRange Exception) Top

When I call the code in my Visual Studio 2005 v. 8.0.50727.42 (En) I get this:
System.FormatException was unhandled
Message="The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.".
I gues is something wrong with the Italian version, try to download C# Express En edition and test it there.


 
 
Enrico Sicignano





PostPosted: .NET Base Class Library, DateTime.Parse problem (IndexOutOfRange Exception) Top

Thanks for your time, I have tried with .NET Framework (EN) but the result is the same.

After many trying, i don't know why, but the code work in a console application.

The problem is that I use this code in a thread launch from Windows Service, in this case the problem is present.

Suggestions

Thanks

-:(



 
 
Samsagaz





PostPosted: .NET Base Class Library, DateTime.Parse problem (IndexOutOfRange Exception) Top

Hi, i've proved that lines of source code in a windows service and i have no errors. It works perfectly.

See ya.


 
 
Enrico Sicignano





PostPosted: .NET Base Class Library, DateTime.Parse problem (IndexOutOfRange Exception) Top

I have resolved the problem in this way:

string[] allPatterns = Thread.CurrentThread.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns();
foreach (string sPattern in allPatterns) {
try {
return DateTime.ParseExact(sLastStart, sPattern, Thread.CurrentThread.CurrentCulture);
}
catch { }
}

Is very strange, but in the list is present a pattern that work and the ParseExact() return a DataTime.

I don't know why



 
 
ShellShock





PostPosted: .NET Base Class Library, DateTime.Parse problem (IndexOutOfRange Exception) Top

A console runs within the profile of the interactive user. A Windows service uses a nominated profile (e.g., the System account), which will often have a different culture. This may explain the differences you saw between the two cases.