Storing and Retrieving values from Session

Storing and Retrieving values in session are quite similar with ViewState. We can interact with Session State with System.Web.SessionState.HttpSessionState class, because this provides built in Session Object with Asp.Net Pages.

Following code is used for storing a value to session

//Storing EmpName in Session
Session[“EmpName”] = txtUser.Text;

Now, let see how we can retrieve values from Session

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

we can also store some other object in Session. Following Example shows how to store a DataSet in session.

//Storing dataset on Session
Session[“DataSet”] = _objDSet;

and following code shows how we can retrieve that dataset from the session

//Check weather session variable null or not
if (Session[“DataSet”] != null)
{
//Retrieving EmpName from Session
DataSet _MyDs = (DataSet)Session[“DataSet”];
}
else
{
//Do Something else
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: