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

Data Transfer Using Cookies Session Variables in ASP.NET 2.0,3.5,4.0 C# VB.NET 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();


Continue



Related Posts:
1. Data Transfer to other aspx pages using Query String Session cookies and cross page posting

2. Cross page posting, Submit Form and Server.Transfer methods in ASP .NET for data transfer

3. LinkButton in GridView and QueryString Parameters to pass/transfer variable and data-ASP.NET Articles
If you like this post than join us or share

2 comments:

Christo said...

Hi,

I have 2 usercontrol in 2 different pages i want data transfer between these user control and pages
i need code for this ,Please give me

Thanks


Anonymous said...

So qrazy..
Mmm..

o matter


Find More Articles