29 July 2013

What is Session ?

Web is Stateless, which means a new instance of the web page class is re-created each time the page is posted to the server. As we all know HTTP is a stateless protocol, it can't hold the client information on page. If user inserts some information, and move to the next page, that data will be lost and user would not able to retrieve the information. So what we need? we need to store information. Session provides that facility to store information on server memory. It can support any type of object to store along with our custom object. For every client Session data store separately, means session data is stored as per client basis. Have a look at the following diagram. 

Storing:
//Storing UserName in Session
       Session["UserName"] = txtUser.Text;

Retriving :

//Check weather session variable null or not
        if (Session["UserName"] != null)
        {
            //Retrieving UserName from Session
            lblWelcome.Text = "Welcome : " + Session["UserName"];
        }
        else
        {
         //Do Something else
        }

 Store DataSet

//Storing dataset on Session
        Session["DataSet"] =ds1;

 Retrive DataSet

  if (Session["DataSet"] != null)
        {
            //Retrieving UserName from Session
            DataSet ds = (DataSet)Session["DataSet"];
        }
        else
        {
            //Do Something else

        }

No comments:

Post a Comment