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");
}
}
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
Showing posts with label State Management. Show all posts
Showing posts with label State Management. Show all posts
Wednesday, June 18, 2008
Session Asp .Net : Storing DataSet
Labels:
Asp .Net,
Session,
Session Variable,
Shalvin,
State Management,
Web Forms Session
Query String Asp .Net : Working with multiple values
Query string is the part of URL that can be used to send data as parameters.
Let's first analyze the query string generated by Google on making a search for 'Shalvin'
http://www.google.co.in/search?hl=en&q=Shalvin.Here two values are passed as query string.
Create a Web site with two web pages.
In the button click of Default.aspx write the following code:
protected void sumButton_Click(object sender, EventArgs e)
{ Response.Redirect("Sum.aspx?Val1=" + txtVal1.Text + "&Val2=" + txtVal2.Text); }
Here is the code for extracting the value in the second page.
protected void Page_Load(object sender, EventArgs e) { Response.Write("Hello " + Request.QueryString["Name"] + " your blog is " + Request.QueryString["Blog"]); }Calculator
<body>
<form id="form1" runat="server">
<div>
Value 1<asp:TextBox ID="txtVal1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="txtVal1" ErrorMessage="Value cannot be string"
Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
<br />
<br />
Value 2<asp:TextBox ID="txtVal2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="txtVal2" ErrorMessage="Value cannot be string "
Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
<br />
<br />
<asp:Button ID="sumButton" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
Default.aspx
protected void Button1_Click(object sender, EventArgs e)
{ Response.Redirect("Sum.aspx?Val1=" + txtVal1.Text + "&Val2=" + txtVal2.Text); }
Sum.aspx
int i, j, res;
protected void Page_Load(object sender, EventArgs e)
{
i = Int32.Parse(Request.QueryString["Val1"]);
j = Int32.Parse(Request.QueryString["Val2"]);
res = i + j;
Response.Write(res.ToString());
}
Related Blog
ASP.Net 2.0 Profile
Subscribe to:
Posts (Atom)