Adsence750x90

Wednesday, July 7, 2010

How to get Session Variable in Class File

How to get Session Variable in Class (.cs) File 

Normally Session is not accessible in Class file. when  one try to call Session in class file it return "Object reference not set to an instance of an object." error description like " An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code." this error is thrown by System.NullReferenceException. To access Session in class file Microsoft introduce a Interface IRequiresSessionState, derived from System.Web.SessionState



 The metadata of IRequiresSessionState


  1. namespace System.Web.SessionState  
  2. {  
  3.     // Summary:  
  4.     //     Specifies that the target HTTP handler requires read and write access to  
  5.     //     session-state values. This is a marker interface and has no methods.  
  6.     public interface IRequiresSessionState  
  7.     {  
  8.     }  
  9. }  


IRequiresSessionState Specifies that the target HTTP handler requires read and write access to session-state values. IRequiresSessionState has no methods.


How to Use



  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.SessionState;  
  6. /// <summary>  
  7. /// Summary description for GetSessionHelper  
  8. /// </summary>  
  9. public class SessionHelper : IRequiresSessionState  
  10. {  
  11.     public SessionHelper()  
  12.     {  
  13.         //  
  14.         // TODO: Add constructor logic here  
  15.         //  
  16.     }  
  17.     /// <summary>  
  18.     /// Get Session values  
  19.     /// </summary>  
  20.     /// <param name="key">session key    /// <returns>object value</returns>  
  21.     public object GetSession(string key)  
  22.     {  
  23.         //check session   
  24.         if (HttpContext.Current.Session[key] != null)  
  25.         {  
  26.             //return session value  
  27.             return HttpContext.Current.Session[key];  
  28.         }  
  29.         else  
  30.         {  
  31.             //return empty string  
  32.             return string.Empty;  
  33.         }  
  34.     }  
  35. }  


Calling SessionHelper Class



  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class Default : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         //Creating object of SessionHelper Class  
  13.         SessionHelper objSessionHelper = new SessionHelper();  
  14.         //setting session value in a variable  
  15.         string sessionValue = objSessionHelper.GetSession("test").ToString();  
  16.         //writing session value in Page  
  17.         Response.Write(sessionValue);  
  18.     }  
  19. }  

2 comments:

Anonymous said...

how to call this session variable in another class,give one example

Anonymous said...

This is a nice solution but I would recommend reviewing this alternative:
http://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net