Category >
ASP.NET
|| Published on :
Monday, November 9, 2015 || Views:
3686
||
hashtable in session hashtable hashtable session
Introduction:-
In this tutorial, we are going learn how to store hashtable in session. By storing values in hashtable & then transfer them to a new page with session is easiest way.
In ASP.NET session state lets you associate a server-side string containing state data with a particular HTTP client session. Session is defined as a series of requests issued by the same client within a certain period of time, and is managed by associating a session ID with each unique client.
So lets starts the coding.:-)
Step 1: First, we start by creating an Empty asp.net web site in Visual Studio .NET 2010.
Step 2: Create an ASPX Page for Right Click on Solution Explorer > Add Items > Web > Webform and save it as "Page1.aspx" and write the below codes in code behind
Note: Please include "System.Collections" namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// source codes donwloaded from www.sourcecodehub.com
Hashtable ht = new Hashtable();
ht.Add("name", "Raja");
ht.Add("phone", "25363636");
ht.Add("address", "23, Redmond, US");
ht.Add("email", "someemail123@email.com");
Session["data"] = ht;
Response.Write("Session created using hash table");
}
}
Step 3: Now we have to read the values stored in the session(the hashtable). For that we have to create another page with name page2.aspx and write the following the codes in code behind file.
Note: Please include "System.Collections" namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// source codes donwloaded from www.sourcecodehub.com
if (Session["data"] != null)
{
Hashtable ht = Session["data"] as Hashtable;
string name = ht["name"].ToString();
string phone = ht["phone"].ToString();
string address = ht["address"].ToString();
string email = ht["email"].ToString();
Response.Write(name + phone + address + email);
}
}
}
Result Screenshots:-
So, In this tutorial we have learned how to store hashtable in session. By storing values in hashtable & then transfer them to a new page with session is easiest way.