I have found a very good solution for this problem which i use regularly in my ASP.NET applications. The solution is to use an "SessionHandler" class in your Application in which one creates properties to access the session variables. By this method we not only are able to access the Session variables through Visual Studio IntelliSense but also apply type safety to them.
Here is an quick example of this class:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Web.UI.HtmlControls;
/*
*
File Name : SessionHandler.cs
Class Name: SessionHandler
Summary : This class is used to handle the session variables and keep a track of them using the
SessionHandler class object.
Advantage is that these variables are shown in VS Intellisense.
And we can also implement type safety.
*
*/
public static class SessionHandler
{
//////////////////////////////
private static string _UserKey = "UserKey";
private static string _Username = "Username";
private static string _LoginType = "LoginType";
//////////////////////////////
public static string UserKey
{
get
{
object obj = HttpContext.Current.Session[
if (obj == null)
{
return string.Empty;
}
else
{
return obj.ToString();
}
}
set
{
HttpContext.Current.Session[
}
}
//////////////////////////////
public static string LoginType
{
get
{
object obj = HttpContext.Current.Session[
if (obj == null)
{
return string.Empty;
}
else
{
return obj.ToString();
}
}
set
{
HttpContext.Current.Session[
}
}
//////////////////////////////
public static string Username
{
get
{
object obj = HttpContext.Current.Session[
if (obj == null)
{
return string.Empty;
}
else
{
return obj.ToString();
}
}
set
{
HttpContext.Current.Session[
}
}
}
Please Note: You need to add this class file to your AppCode folder in your Web Application.
No comments:
Post a Comment