Reputation: 425
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!
Upvotes: 0
Views: 450
Reputation: 1048
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);
}
Upvotes: 1