Question:
in web.config
in section
system.web
I have
<globalization culture="de-ch" uiCulture="de-ch" requestEncoding="UTF-8" responseEncoding="UTF-8"/>
What I want is to parse a string like this
"20.03.2012 00:00:00"
to a datetime value
but
DateTime dtAsIs = DateTime.Parse("20.03.2012 00:00:00")
throws an exception
Unfortunately only on the testserver, not on my development system.
I do not have access to the testserver, except to copy the webapp over into a windows share.
I can reproduce the exception like this:
DateTime dtThrowsException = DateTime.Parse("20.03.2012 00:00:00",new System.Globalization.CultureInfo("en-us"));
Whereas it works fine like this:
DateTime dtWorks = DateTime.Parse("20.03.2012 00:00:00",new System.Globalization.CultureInfo("de-ch"));
I checked the ASP page, and there is NO culture set in the asp page
(I mean this:
<% @Page Culture="fr-FR" Language="C#" %>
)
If I set
System.Threading.Thread.CurrentThread.CurrentCulture
and
System.Threading.Thread.CurrentThread.CurrentUICulture
to de-ch at the very start of Page_Load like this
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-ch");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-ch");
then it works fine.
The browser language is set to “de-ch”, I checked that.
Can anybody tell my why the thread-culture gets set to English ?
I mean the obvious reason is that the server operating system is English, but I can’t change that one, only settings in web.config.
The problem seems to be that ASP.NET overwrites the culture even when you explicitly specify it.
(Like
)
one needs to force override it
So in order to make it configurable and change it as few as possible, you need to get the culture from web.config with
and then force set it with
Note the overload with false, it’s necessary, otherwise it doesn’t really work.
Here is my solution:
Then, in the codebehind, replace System.Web.UI.Page with PageWithCorrectPageCulture
And for those who can only copy-pase C#: