views:

106

answers:

2

I have the following code to store session variable in one webmethod and retrieve it in other webmethod but the value displays null when i try to retrieve it.

[WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public bool SubmitList1(string businessname ) {

    Session["Company_Name"] = businessname;  

    SqlConnection con = new SqlConnection();
         .......
       .........
            .........

}

This will be my second webmethod where i am trying to retrieve the session variable [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public bool addresslisting() {

    string companyname = (string)Session["Company_Name"];// this particular value is displaying null
   ......
       ........

}

A: 

I think you can use : Context.Session["Company_Name"] = businessname and keep [WebMethod(EnableSession = true)] on each webmethod.

Also, make sure that in the web.config, configuration>system.web>sessionState mode is enabled. If you turn off the sessionstate from web.config, then also the session state can not work.

If you dont want to use Session state, the you may consider writing the value into a XML file with UserId/SessionId as parent element and then read it whenever needed.

Siva Gopal
Writing xml you mean to say construct xml and store that to server and access from other webmethod ?
mahesh
In my webconfig file i dont have sessionState do we need to add any library?
mahesh
You need to have : <configuration><system.web><sessionState mode="InProc"/></system.web></configuration> . For this you need not add any reference. Keeping the sessionState mode="InProc" will allow you to use HttpSessionState object which will reside in-memory. Please let me know the result, once you add the above config secion into your web.config.
Siva Gopal
For writing XML, what you understood is what i mean. :-)
Siva Gopal
A: 

This is a double-post to http://stackoverflow.com/questions/3717437/how-to-exchange-session-or-cookie-variables-between-two-webmethods-in-asp-net-web , so here it comes again:

Disclaimer: I think that a web service that relies on Session state is just plain WRONG since a web service should be stateless. However:

At http://msdn.microsoft.com/en-us/library/aa480509.aspx you can read about how to use ASP.NET Session in a web service:

  1. Make sure that /configuration/system.web/sessionState in web.config is configured properly to enable session state
  2. Make sure that uses the web service has a cookie container where the ASP.NET session cookie can be stored. If the client is from a web browser (e.g. ajax call) this usually works out of the box, but if you are building a standalone client, you have to do some more work, see the link above.

All in all: a bad design decision gives you more work than necessary (sorry for rubbing it in).

I think you should redesign you web service so that you either always send username and password in all methods or add a login method that gives the client a token that is sent with each web service request.

Andreas Paulsson