Wednesday, June 18, 2008

Session Asp .Net : Storing DataSet

This blog is an add on to my youtube vide ASP.Net (VB.Net) Session Variable ArrayList - Shalvin
Session state allows values to be stored in one page and accessed through out the site. Session state allows complex data to be stored whereas Query String supports only string value.
The session data is stored in the memory of Web Server.

Asp .Net uses an unique 120 bit identifier for tracking sessions. This ID is the only piece of information that is transmitted between the web server and the client. When the client presents the session ID, ASP.NET looks up the corresponding session, retrieves the serialized data from the state server, converts it to live objects, and places these objects into a special collection so they can be accessed in code. This process takes place automatically.
It is possible to store large amounts of data in a Session Variable. In this example I am constructing a dataset and assigning the contents to a Session variable and passing the showing the data of session variable on another page.

This example makes use of appSettings section of web.config to store connectionstring information. If you are not familiar with appSettings visit my blog Web.config's appSettings section to avoid hard coded memory variable

//web.config




//Default.aspx
using System.Configuration;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
cnn = new SqlConnection(ConfigurationManager.AppSettings.Get("Cnn"));
cnn.Open();

da = new SqlDataAdapter("select ProductName, UnitPrice from Products", cnn);
da.Fill(ds, "Prod");
Session["Prod"] = ds.Tables["Prod"];
}
protected void btnShowCart_Click(object sender, EventArgs e)
{
Response.Redirect("ShoppingCart.aspx");
}
You need to cast the Session data to DataSet inorder to bind it to GridViewShoppingCart.aspx
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = (DataTable)Session["Prod"];
DataBind();
}

Related Blogs
Query String Asp .Net : Working with multiple values

ASP.Net 2.0 Profile

2 comments:

  1. thank you sir,

    this is usefull article for me, becouse i am using lot of sessions related to asp.net. pls blog how to update the gridview rows using dataset.

    madhu, dubai

    ReplyDelete
  2. Thank you Sir..
    Im just a beginner.. i was searching various sites for understanding session concept..
    Finally you gave me a short & straight solution..

    MRam..

    ReplyDelete