krafo
krafo

Reputation: 183

Session variable + WebMethod variable value not updating

I have a web app with a dropdown list. When a new index is selected I have to storing the value to a session variable, which is created on Session_Start event.

protected void Session_Start(object sender, EventArgs e)
{
    Session.Add("testValue", "test");
}

On selectedindex changed event i'm setting the new value like this

Session["testValue"] = DropDownList.SelectedItem.Text;

I have a web service where I retrieve the value of the session variable like this:

[WebMethod(EnableSession = true)]
public string getValue()
{

    var testVal = Session["testValue"].ToString();

    return testVal.ToString();
}

From a console app I connect to the web service and retrieve the value returned by getValue(), however the initial value is always being returned. any idea please?

Upvotes: 2

Views: 4559

Answers (3)

krafo
krafo

Reputation: 183

The issue is because when the console app is run it seems that a new session is created. Using Application state using Application.Set and Application.Get solved the issue. Hopefully i will not have issues when the system will be used by multiple users.

Upvotes: 1

Linga
Linga

Reputation: 10563

Here the values are not changed, You didn't change the values. So nothing expected

public string getValue()
{
    var testVal = Session["testValue"].ToString();
    return testVal.ToString();
}

The mistake is probably in dropdownlist

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
  Session["testValue] = dropdownlist1.SelectedItem.text;
}
}

And,

protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["testvalue"] = dropdownlist1.SelectedItem.text;
}

Also try with

System.Web.HttpContext.Current.Session["testvalue"] 

in both parts

Upvotes: 0

Srinivas
Srinivas

Reputation: 1063

Check whether the values of the items in your dropdown list are different.This is essential for your selected index changed event to be fired.

Upvotes: 0

Related Questions