473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to set and get cookie using c#?

nirmalsingh
218 100+
hai all, i am using c#.net, i want to store a value in a cookie and want to get it at when i needed. i used the following code
to set
Expand|Select|Wrap|Line Numbers
  1.  HttpCookie cook = new HttpCookie("UserInfo");
  2.  cook.Values.Add("USER_NAME", txtUserName.Text.Trim());
  3. cook.Values.Add("PASSWORD", txtPassword.Text.Trim());
  4.  Response.AppendCookie(cook);
  5.  
to get
Expand|Select|Wrap|Line Numbers
  1. if (Request.Cookies["USERNAME"] != null && Request.Cookies["PASSWORD"] != null)
  2.  {
  3.  txtUserName.Text = Request.Cookies["USERNAME"].ToString();
  4. txtPassword.Text = Request.Cookies["PASSWORD"].ToString();
  5. }
  6.  
but it is not working
plz help me,
thanx in advance
Apr 9 '08 #1
1 9853
balame2004
142 100+
hai all, i am using c#.net, i want to store a value in a cookie and want to get it at when i needed. i used the following code
to set
Expand|Select|Wrap|Line Numbers
  1.  HttpCookie cook = new HttpCookie("UserInfo");
  2.  cook.Values.Add("USER_NAME", txtUserName.Text.Trim());
  3. cook.Values.Add("PASSWORD", txtPassword.Text.Trim());
  4.  Response.AppendCookie(cook);
  5.  
to get
Expand|Select|Wrap|Line Numbers
  1. if (Request.Cookies["USERNAME"] != null && Request.Cookies["PASSWORD"] != null)
  2.  {
  3.  txtUserName.Text = Request.Cookies["USERNAME"].ToString();
  4. txtPassword.Text = Request.Cookies["PASSWORD"].ToString();
  5. }
  6.  
but it is not working
plz help me,
thanx in advance

Hi,

Carefully look into your code. Could you find where the error happen?

You are using cookie name "USER_NAME" when you set cookie. However you are using cookie name "USERNAME" when you get cookie. This conflict is the reason why your code is not working.

Always use same cookie name.
Apr 10 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ajay | last post by:
hi! i am printing a simple cookie, but instead of printing um=name:blah&access:admin&exp:2312390.909 its printing um="name:blah&access:admin&exp:2312390.909" why the quotes?
2
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
4
by: Wally | last post by:
I have a record set (rs) that contains 25 barcodes values that I set to true in a cookie. (see code section below) If I read the cookie from within the same page that created it, I see all 25...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
6
by: Denna | last post by:
Hello, I'm having a little trouble with cookies, I can write one when I press a button using the following code:- HttpCookie myCookie = new HttpCookie("MyTestCookie"); myCookie.Value =...
1
by: Joe | last post by:
Hi folks, Been looking at this for the day so any ideas would be appreciated. Here's the scenario, bear with me - it looks more complicated than it is :) I have a page templating system that...
1
by: CR1 | last post by:
I found a great cookie script below, but don't know how to make it also pass the values sent to the cookie, to a querystring as well for tracking purposes. Can anyone help? If there was a way to...
15
by: Edwin Knoppert | last post by:
I have searched but info is limitted. In my test app i used a non persistant cookie for forms authentication. slidingExpiration is set to true On run and close and rerun the login remains ok....
7
by: =?Utf-8?B?SlA=?= | last post by:
I need to design a WS that will after authenicating the user, create a cookie on the users PC that made the request. All the code I keep finding is how to get a WS to read a cookie, I need it to...
2
by: kelly.pearson | last post by:
Is this a bug? I am trying to write a cookie that can be accessed by various .Net applications on our domain. However, whenever I add the domain property to the cookie, no errors get thrown but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.