Different ways of using a Session for State Management


In this article we are going to see how can we use the Session State Management technique to store the different types of data in that and transfer it to different page within the website and use it in our website.It is going to be a very good example who want to learn Session concept in Asp.Net.

We know that Session variable is used to store the value in one page and use it in other page within that particular session. Here is some examples of using the Session variable for storing the value in different formats.
Like

1. Storing a normal variable in Session.
2. Storing the class in the Session.
3. Storing the Collection of string in Session as StringCollection.

Storing variable in Session

1. Take 2 pages Login.aspx and welcome.aspx
2. Add 2 label(text= enter uid and Enter Password)
2 textbox(id = txt_uid and txt_password)
1 button(id= but_submit)

3. Write below code on Button click event:


protected void but_submit_Click(object sender, EventArgs e)
{

sqlconn.Open();
SqlDataAdapter da = new SqlDataAdapter(("select UserID,Pass_word from users where UserID = '" + txt_uid.Text + "' and Pass_word = '" + txt_password.Text + "' "), sqlconn);
DataSet ds = new DataSet();
da.Fill(ds, "temp1");
if (ds.Tables["temp1"].Rows.Count > 0)
{
//store usedid in the session as variable
Session["userid"] = ds.Tables["temp1"].Rows[0]["UserID"];
Response.Redirect("welcome.aspx");


}
else
{

Response.Redirect("login.aspx");

}


4. On welcome page write the below code:

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Welcome " + Session["userid"].ToString();
}

Storing Class In A Session

1. Create a class Employee.Write the below code inside it:

// define 2 public members eid and ename and a empty constructor.
public class Emp
{
public int eid;
public string ename;
public Emp()
{

}
}


2. Design a page having 2 labels(text= Enter empid and Enter empname)
2 textbox(id = txt_empid and txt_empname)
1 button(id =but_submit)

3. Write below code on button click event:

protected void but_submit_Click(object sender, EventArgs e)
{
Emp e1 = new Emp();
e1.eid = Convert .ToInt32 (txt_empid.Text);
e1.ename = txt_empname.Text;
// store the employee class in Session
Session["employee"] = e1;
Response.Redirect("welcome.aspx");

}


4. Write the below code on the welcome page Pageload event:

protected void Page_Load(object sender, EventArgs e)
{

Emp em = (Emp )Session["employee"];

Response.Write("Employee Details Are :
");
Response.Write("
Emp Id :" + em.eid.ToString());
Response.Write("
Emp Name :" + em.ename.ToString());
}



Storing StringCollection in Session

In this we store the value of listbox in the Session as collection and then on ther page we display it inside listbox.

1. Take 2 page source.aspx and destination.aspx
2. Take 1 button(id = but_submit) and 1 listbox(id=listbox1) in source page and 1 listbox(listbox2) in destination page.
3. Add below code on button click of source page:

protected void Button1_Click(object sender, EventArgs e)
{
StringCollection States = new StringCollection();

foreach (ListItem i in listbox1.Items )
{
//Stores all the items in the listbox to the "State" collection.
States.Add(i.ToString());

Session["Stateslist"] = States;
}


}


4. On destination pageload event add the below code:

protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear();

if (Session ["Statelist"] != null )
{
//copis all the data in session to the state stringcollection.
StringCollection State = (StringCollection )Session["Statelist"];

foreach (string i in State )
{
ListBox1.Items.Add(i);
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: