This is silly, each browser Culture (‘en-US’ or ‘en-CA’) define dates differently

I have a function call to a stored procedure, and it kept failing (exception thrown: Invalid Value NVARCHAR). After rigorousness testing I found that browser Culture is to culprit. I found that “en-CA” date format was different from “en-US“. The “en-CA” was sending the wrong date format to my stored procedure thus it kept failing.

en-CA“: 14/04/2011
en-US“: 4/14/2011

So here is a fix, this can be global (BasePage.cs) or just for a single page (page.aspx.cs).

protected override void InitializeCulture(){
  CultureInfo cultureInfo;

  // Set cookie to "en-US" lang culture
  if (Session[WebConstants.LANGUAGE] == null){

    string cookieLang = myCookies.GetLanguage(Request);
	if (cookieLang == null || cookieLang == ""){
       cultureInfo = new CultureInfo("en-US", true);
    } else {
	   cultureInfo = new CultureInfo(cookieLang, true);
	}

  } else {

    cultureInfo = new CultureInfo((string)Session[WebConstants.LANGUAGE], true);

  }

  Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", true);
  Thread.CurrentThread.CurrentUICulture = cultureInfo;
  currentCultureName = cultureInfo.Name;
  base.InitializeCulture();

}

Best practice is to use a cookie to remember your language preference.

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Advertisements
%d bloggers like this: