Before proceeding, you should follow my ASP.NET Web Service series from the following links,
To use ASP.NET Session object in a Web Service, there are two things that we need to do. The Web Service class should inherit from System.Web.Service.WebServices class. If we remove the parent class, we can’t use Session object.

Second thing is that we need to set EnableSession property equal to true of the WebMethod attribute.

Now, I’ll work on a previous project of the Web Service. Flip to Visual Studio. I want to show all the results a user has performed in a grid view, using session object, so let’s go for it.
Here, in MyWebService.asmx file, I’ve two methods AddName() and Sub(). I’m going to perform all the operations in AddName method. In previous articles, I worked on Sub() method.
Hence, within AddName() method, first create a variable of type list of string and call it a result. I’m using Session object within this method. Let’s use a Session variable and call it RESULT. If there is Sessio the RESULT is null, in which I’m going to create a list of strings. I want to show -
First Name (Amit) + Second Name (Mishra) = Amit Mishra
Let’s understand the code that we are going to use.
In order to achieve that, use the code given below -
- string strResult=FirstName.ToString() + " + " +SecondName.ToString() +
- " = " + (FirstName+SecondName).ToString();
- Session["RESULT"]=result;
- public string AddName(string FirstName, string SecondName) {
- List < string > result;
- if (Session["RESULT"] == null) {
- result = new List < string > ();
- } else {
- result = (List < string > ) Session["RESULT"];
- }
- string strResult = FirstName.ToString() + " + " + SecondName.ToString() +
- " = " + (FirstName + SecondName).ToString();
- result.Add(strResult);
- Session["RESULT"] = result;
- return FirstName + SecondName;
- }
- [WebMethod(EnableSession = true)]
- public List < string > GetResult() {
- if (Session["RESULT"] == null) {
- List < string > result = new List < string > ();
- result.Add("Hey!!!!! you are unable to perform any action");
- return result;
- } else {
- return (List < string > ) Session["RESULT"];
- }
- }

























Hadshana KamalanathanPosted Jul 22, 2018, 1:31 AM
Good one..
SubashPosted Mar 17, 2017, 12:17 AM
Nice Explanation thank you sir
Santhakumar MunuswamyPosted Jun 25, 2016, 3:17 AM
Nice share
Thiruppathi RPosted Jun 15, 2016, 1:35 AM
Great ..
Kuppurasu NagarajPosted Jun 14, 2016, 1:18 PM
Nice Sharing..
Vignesh ManiPosted Jun 11, 2016, 4:31 PM
nice one
RajaPosted Jun 11, 2016, 6:23 AM
Nice Share...
Debasis SahaPosted Jun 11, 2016, 4:40 AM
Nice one..