2

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?

4
  • check whether the values of the items in your dropdownlist are different Commented Apr 8, 2013 at 12:28
  • You say from a console app - are you expecting your console app and webbrowser to share the same session ? Commented Apr 8, 2013 at 12:42
  • Hi Richard, my intention is to have the webMethod retrieve the session variable value, which currently is doing when i access the webservice.asmx and invoke the method the value is returned accordingly. Shouldnt it be the same behaviour when the url is accessed from the console app? Commented Apr 8, 2013 at 12:56
  • Remember to enable session in web.config <sessionState mode="InProc"/> Commented Aug 21, 2014 at 19:53

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

4 Comments

that method should not change the value of the session variable but retrieve it. the value of session variable is changed on SelectedIndexChanged event of DropDown
the session variable value is changed accordingly since when i access the webservice through its url and invoke the method it returns the value chosen by the client. the issue when accessing the webmethod from a console application.
Try with static method. +1
if you mean the webmethod as static, then the webmethod would not be available when consuming the webservice from the console app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.