IT Community - Software Programming, Web Development and Technical Support

How to use cookie in asp.net using C#?.Is there any sample for cookies?

This is a discussion on How to use cookie in asp.net using C#?.Is there any sample for cookies? within the ASP and ASP.NET Programming forums, part of the Web Development category; How to use cookie in asp.net using C#?.Is there any sample for cookies?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-30-2007, 12:46 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Question How to use cookie in asp.net using C#?.Is there any sample for cookies?

How to use cookie in asp.net using C#?.Is there any sample for cookies?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 12:53 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: How to use cookie in asp.net using C#?.Is there any sample for cookies?

In order to handle a cookie in your system you should aware of 2 things:

1 - How to create them

2 - How to delete them

So, first in order to create a cookie you have 2 variable must be defined as they are the cookie name and the duration of the cookie before it expired

• In order to create a cookie it looks like this one

////SET THE COOKIE////

Response.Cookies["test"].Value = UserName.Text ;

Response.Cookies["test"].Expires = DateTime.Now.AddYears(30);

Where test is the cookie name and the username.text is the textbox that will be used to define the cookies information.

* In order to delete a cookie use this

Response.Cookies["test"].Expires = DateTime.Now.AddYears(-30);

* In order to check whether the cookie is found or not and retrieve the cookies information use this

////check to see if cookie presented////

if (Request.Cookies["test"] == null)

TextBox2.Text = "No cookie found";

else

TextBox2 .Text = Request.Cookies["test"].Value;

////END check to see if cookie presented////

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Cookies["ashok"].Value = TextBox1.Text;
Response.Cookies["ashok"].Expires = DateTime.Now.AddSeconds(6);
}
}
protected void Button1_Click(object sender, EventArgs e)
{

if (Request.Cookies["ashok"] != null)
Label1.Text = Request.Cookies["ashok"].Value;
else
Label1.Text = "Cookie Not Found";


}
}
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-09-2007, 08:01 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Default Re: How to use cookie in asp.net using C#?.Is there any sample for cookies?

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["MyCookie"]["Data"] = TextBox1.Text;
Response.Cookies["MyCookie"]["Time"] = DateTime.Now.ToString("G");
Response.Cookies["MyCookie"].Expires=DateTime.Now.AddMonths(1);
Label1.Text = "Cookie created!<p>" + "Your cookie contains:<font color=red>" + Request.Cookies["MyCookie"]["Data"] + "<br>" + Request.Cookies["MyCookie"]["Time"] + "</font>";
Response.Cookie("MyCookie").Expires=DateTime.FromS tring("2006-10-1");
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Request.Cookies["MyCookie"] == null)
Label1.Text = "There is no cookie:";
else
Label1.Text = "Your cookie contains:" + "<font color=red>" + Request.Cookies["MyCookie"]["Data"] + "<br>" + Request.Cookies["MyCookie"]["Time"] + "</font>";

}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-13-2007, 09:36 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Default Re: How to use cookie in asp.net using C#?.Is there any sample for cookies?

Hi All..

I am using shoping cart detail saved in cookie, the user selected product id stored in cookie, i can get all the value from cookie, but i can get productId for current and previous values only, i can't get all productid...


pls gimme solution...


Thanks
__________________
H2O

Without us, no one can survive..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-19-2007, 08:09 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Red face Re: How to use cookie in asp.net using C#?.Is there any sample for cookies?

Quote:
Originally Posted by kingmaker View Post
How to use cookie in asp.net using C#?.Is there any sample for cookies?
Hi buddy,

Here's a tutorial that shows you how to use cookies in ASP .NET. I'm not going to explain the role of cookies in web applications or cover any other theoretical aspect of cookies. There are many (similar) ways to handle cookies in ASP .NET. I'm only going to show you one of the ways, my way. Oh, and we're going to use C#, although the code can be adapted to Visual Basic .NET easily.

How to create a cookie.

Here's a new cookie named cakes.

Code:
HttpCookie myCookie = new HttpCookie("cakes");
We created the cookie but there are no keys with values in it, so for now it's useless. So let's add some:

Code:
myCookie.Values.Add("aaa", "favour");

myCookie.Values.Add("bbb", "secret");
We also need to add the cookie to the cookie collection (consider it a cookie jar ):

Code:
Response.Cookies.Add(myCookie);
How to get the value stored in a cookie.

Here's how to get the keys and values stored in a cookie:

Code:
Response.Write(myCookie.Value.ToString());
The output to using this with the previous created cookie is this: "aaa=favour&bbb=secret".



However, most of the time you'll want to get the value stored at a specific key. If we want to find the value stored at our bbb key, we use this:

Code:
Response.Write(myCookie["bbb"].ToString());
That's all....
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/asp-asp-net-programming/2996-how-use-cookie-asp-net-using-c-there-any-sample-cookies.html
Posted By For Type Date
cookie This thread Refback 08-17-2007 04:16 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
sample for load *.OBJ file using Android Object3D class leoraja8 Mobile Software Development 0 01-09-2008 01:52 AM
Is there any sample code for sending a text file from the phone to the PC via... mobilegeek Mobile Software Development 4 09-11-2007 04:12 AM
Write sample code for pagination using java script itbarota HTML, CSS and Javascript Coding Techniques 1 09-11-2007 12:00 AM
Any simple step or sample code for transparency mobile..? mobilegeek Mobile Software Development 1 07-24-2007 06:43 AM
sample script in Rational Robot vigneshgets Software Testing 0 05-22-2007 08:25 AM


All times are GMT -7. The time now is 01:42 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0