0

As the title says, im trying to create an CultureInfo object and save its value in a session. And using that saved CultureInfo object in my method for the returned value. But I get this error, and I cant find the solution! Please take a look.

Class:

public class DateTimeService : WebService
{
    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = true)]
    public string FormatDate(string dateString)
    {
        DateTime date;
        var ci = new CultureInfo(Session["Format"].ToString()); //Culture is not supported. 
        var formats = Session["Format"].ToString();
        DateTime.TryParseExact(dateString, formats, ci, DateTimeStyles.None, out date);
        return date.ToString(ci);
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = true)]
    public void SetFormat(string formatString)
    {
        Session["Format"] = formatString;
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = true)]
    public void SetCulture(string language)
    {
        if (language == "sv-SE")
        {
            Session["CultureValue"] = new CultureInfo("sv-SE", false);
        }

        if (language == "en-US")
        {
            Session["CultureValue"] = new CultureInfo("en-US", false);
        }
    }

Global.asax (Where I apply a default Session value):

public class Global : System.Web.HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {

    }

    protected void Session_Start(object sender, EventArgs e)
    {
        Session["Format"] = ("ddMMYYYY");
    }

 }

Help would be much appreciated!

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

1

Two problem:

You're using the "Format" session variable, shouldn't that be the "CultureValue" one?

new CultureInfo(Session["Format"].ToString());

You should cast the session variable, like it's done here.

I also don't see the point of this:

public void SetCulture(string language)
{
    if (language == "sv-SE")
    {
        Session["CultureValue"] = new CultureInfo("sv-SE", false);
    }

    if (language == "en-US")
    {
        Session["CultureValue"] = new CultureInfo("en-US", false);
    }
}

Unless you want to limit the possibilities of which "languages" (actually they are CultureInfo names in the "languagecode2-country/regioncode2" format) are allowed, this code isn't very useful and can be simplified to:

public void SetCulture(string language)
{
    Session["CultureValue"] = CultureInfo.CreateSpecificCulture(language);
}
Community
  • 1
  • 1
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35
  • I changed the Session, and it now says. "Object reference not set to an instance of an object." I dont understand what you meant by your second problem solution though. Could you be more specific= – Andre Korosh Kordasti Nov 03 '14 at 12:57
  • You'll need to do `var ci = (CultureInfo)Session["CultureValue"];` But of course you will need to check that `Session["CultureValue"]` is not NULL before you do so. When do you call `SetCulture(string language)`? – BCdotWEB Nov 03 '14 at 13:00
  • When the user types in a language, the SetCulture method is called. So: "var ci = (CultureInfo)Session["CultureValue"];" is still null. Why? – Andre Korosh Kordasti Nov 03 '14 at 13:15
  • I'd set a default `Session["CultureValue"]` in `Session_Start` if I were you, that way you're sure there is a value when you use it. Same logic you have for `Session["Format"]`. – BCdotWEB Nov 03 '14 at 13:21
  • Ok so I gave it a default value, but then I got this error. "Unable to cast object of type 'System.String' to type 'System.Globalization.CultureInfo'" at "var ci = (CultureInfo)Session["CultureValue"];"... – Andre Korosh Kordasti Nov 03 '14 at 13:35
  • Did you do something like this: `Session["CultureValue"] = CultureInfo.CreateSpecificCulture("en-US");`? – BCdotWEB Nov 03 '14 at 13:42
  • Yes, I wrote: "Session["CultureValue"] = CultureInfo.CreateSpecificCulture("sv-SE");" in Session_Start method. The value that's returned is: "0001-01-01 00:00:00" This is very strange. – Andre Korosh Kordasti Nov 03 '14 at 13:47