Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Archive for October 5th, 2008

SessionWrapper Class in C#

Posted by Ramani Sandeep on October 5, 2008

ASP.NET provides mechanisms for storing information for a single user session or across multiple sessions.This is done using the HttpSessionState and HttpApplicationState classes.The Page class has Application and Session attributes to provide access to current objects.

The simple way to access them is as following:

if (Session["FirstName"] == null)

{

    LabelFirstName.Text = “FirstName”;

}

else

{

    LabelFirstName.Text = (string)Session["FirstName"];

}

if (Session["LastName"] == null)

{

    LabelLastName.Text = “LastName”;

}

else

{

    LabelLastName.Text = (string)Session["LastName"];

}

This method has several drawbacks:

Type Safety: Session contains objects, so you need to cast.

Null reference: You must check, it might be null

Variable name: don’t use hard coded strings, beware of typos…

So Lets see the wrapper object and how it solves those issues:

public static class SessionWrapper

{

    private static T GetFromSession<T>(string key)

    {

        object obj = HttpContext.Current.Session[key];

        if (obj == null)

        {

            return default(T);

        }

        return (T)obj;

    }

    private static void SetInSession<T>(string key, T value)

    {

        if (value == null)

        {

            HttpContext.Current.Session.Remove(key);

        }

        else

        {

            HttpContext.Current.Session[key] = value;

        }

    }

    private static T GetFromApplication<T>(string key)

    {

        return (T)HttpContext.Current.Application[key];

    }

    private static void SetInApplication<T>(string key, T value)

    {

        if (value == null)

        {

            HttpContext.Current.Application.Remove(key);

        }

        else

        {

            HttpContext.Current.Application[key] = value;

        }

    }

    public static string FirstName

    {

        get { return GetFromSession<string>(“FirstName”); }

        set { SetInSession<string>(“FirstName”, value); }

    }

    public static string LastName

    {

        get { return GetFromSession<string>(“LastName”); }

        set { SetInSession<string>(“LastName”, value); }

    }

    public static User User

    {

        get { return GetFromSession<User>(“User”); }

        set { SetInSession<User>(“User”, value); }

    }

}

It contains a few generic private functions to read and write objects from/to the Session or the Application objects, and public methods to be used from the code to access required properties. Note that the use of HttpContext.Current requires a valid request, or Current might be null!

This wrapper provides a safe typed access from all over the application, one place to define all objects that might be stored in the session. If you want to move objects from the Session to other storing mechanism this would be solved in the wrapper only.

The usage is also much easier:

LabelFirstName.Text = SessionWrapper.FirstName;

LabelLastName.Text = SessionWrapper.LastName;

Note that you can add any initialization code or default values in the wrapper:

public static string FirstName

{

    get

    {

        string firstName = GetFromSession<string>(“FirstName”);

        if( string.IsNullOrEmpty(firstName))

        {

            firstName = “FirstName”;

            SetInSession<string>(“FirstName”, firstName);

        }

        return firstName;

    }

    set { SetInSession<string>(“FirstName”, value); }

}

Posted in ASP.NET, C# 2.0 | Tagged: | Leave a Comment »

Change Row Color of Gridview in ASP.NET 2.0

Posted by Ramani Sandeep on October 5, 2008

Method – 1 : DataBound Event

This article is used to Change Color of Gridview datarow when data is binding.

When Date in Gridview datarow’s field is today’s date.

Note : lblPostedOn is label which boundfield which is of date type.

protected void gvQuery_DataBound(object sender, EventArgs e)
{
Label sDate = null;

foreach (GridViewRow r in gvQuery.Rows)
{
sDate = (Label)r.FindControl(“lblPostedOn”);
System.DateTime dt = Convert.ToDateTime(sDate.Text);
if (dt.Date == System.DateTime.Today.Date)
r.BackColor = System.Drawing.Color.Gray;
}

}

Method – 2 : RowCreated Event

Here’s the code for that :

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add(“onMouseOver”, “this.style.background=’#eeff00′”);
e.Row.Attributes.Add(“onMouseOut”, “this.style.background=’#ffffff’”);
}

Posted in ASP.NET, C# 2.0 | Tagged: | Leave a Comment »