Sessions:
Sessions can be used to store even complex data for the user just like cookies.



Sessions can be used easily in ASP.NET with the Session object. We will re-use the sessions . Keep in mind though, that sessions will expire after a certain amount of minutes, as configured in the web.config file.
<sessionState timeout="600"/>

The Session context not only can hold values across page requests, it can also first fire events on Session Start and on Session End. These will be very useful in setting up our tracking object and handling notification and reporting.

In Login.aspx


 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
Session.Clear();          // Clears the Session Data
Session.Abandon();        // Clears the session objects completly
string username=txtusername.Text;
                Session["username"] = username.ToString();
            }
           
        }
        catch
        {

        }
    }
We can access this value in any page where ever we want untill the session will expire and session data will clear by us.


In Home.aspx:


protected void Page_Load(object sender, EventArgs e)
    {
        try
        {           
string username=Session["username"].ToString();
                lbluserName.Text = username;           
           
        }
        catch
        {

        }
    }


Share it

1 comments:

  • sarabjeet said... November 22, 2012 at 5:14 PM

    Great work, really appreciate your effort for the information and being enrolled in http://www.wiziq.com/course/46-INTERVIEW-PREPARATION-COURSE-BY-SHIVPRASAD-KOIRALA the OOps, .NET and SQL INTERVIEW Preparation i found your information very helpful.

Post a Comment