views:

108

answers:

2

I'm developing a shopping cart, and part of the functionality is to select your currency.

I have a drop down list, and when the selected index changes I've written some code to find the culture code (such as en-GB or en-US) and then change the culture of the current session. In addition, using the exchange rates given it changes the price...

I currently have en-GB as the default culture. When someone selects the en-US culture from the drop down everything works fine. The currency changes (all currency labels are set with ToString("C")) and the exchange rate changes.

When I use the drop down list to select en-GB again, the exchange rate changes (so I know the code is working), and after debugging I can see that the culture session has changed from en-US to en-GB, but the currency still displays as $ and not £.

I really can't understand why this is happening. The code is very simple, I'm overriding the page_Load event for each page to display the correct currency based on culture:

protected override void OnLoad(EventArgs e)
{

    if (Session["Culture"] != null)
    {
        string culture = Session["Culture"].ToString();
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["Culture"].ToString());
    }

    base.OnLoad(e);
}

Why does it not change the currency back to £ when I'm changing the session culture from en-US to en-GB?

+1  A: 

Because you are checking if (Session["Culture"] != null), which restricts you to set the culture second time as Session["Culture"] will not be null.

Ravia
If I create another culture, such as fr-CH for Swiss Francs, I can change between USD and Swiss Francs no problem. It updates the currency symbol, it's only when changing back to en-GB for some reason. It displays $ and not £.
Paul
+1  A: 

I got it, because it was coming from a database entry that for some reason was a char(10) datatype, I forgot to trim the text. This resulted in the culture not being found by the Framework and defaulting back to en-US as it couldn't find 'en-GB '.

Paul