Monday, August 23, 2010

Data Transfer Using Cookies Session Variables Data Transfer Using Cookies Session Variables in ASP.NET C# VB.NET

Data Transfer Using Cookies

Cookies are stored in client machine and can have max size of 4kb
To store value from textbox.

protected void btnGo_Click(object sender, EventArgs e)
{
HttpCookie MyCookie = new HttpCookie("Data");
MyCookie.Value = txtData.Text;
MyCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(MyCookie);
Response.Redirect("Deault2.aspx");
}
And to retrieve data from cookie in Deault2.aspx page
In the Page_Load event write
String strData = Request.Cookies["Data"].Value);

Data Transfer using Session Variables

Storing and retrieving Data to and from
session variables on the page we want.

To store a value in session we need to write
protected void btnGo_Click(object sender, EventArgs e)
{
Session["VariableName"] = txtData.Text;
Response.Redirect("Deault2.aspx");
}
To retrieve value from Session on other page
string strData = Session["VariableName"].ToString();
Don't forget to use proper typecasting as session stores data as Object and we need
to change it to desired type
To remove Session Variables use
Session.Contents.Remove("VariableName"); or
Session.Contents.RemoveAll(); or
Session.Abandon();

No comments:

Post a Comment