how to set and get cookie using c#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    how to set and get cookie using c#?

    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
    Code:
     HttpCookie cook = new HttpCookie("UserInfo");
     cook.Values.Add("USER_NAME", txtUserName.Text.Trim());
    cook.Values.Add("PASSWORD", txtPassword.Text.Trim());
     Response.AppendCookie(cook);
    to get
    Code:
    if (Request.Cookies["USERNAME"] != null && Request.Cookies["PASSWORD"] != null)
     {
     txtUserName.Text = Request.Cookies["USERNAME"].ToString();
    txtPassword.Text = Request.Cookies["PASSWORD"].ToString();
    }
    but it is not working
    plz help me,
    thanx in advance
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    Originally posted by nirmalsingh
    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
    Code:
     HttpCookie cook = new HttpCookie("UserInfo");
     cook.Values.Add("USER_NAME", txtUserName.Text.Trim());
    cook.Values.Add("PASSWORD", txtPassword.Text.Trim());
     Response.AppendCookie(cook);
    to get
    Code:
    if (Request.Cookies["USERNAME"] != null && Request.Cookies["PASSWORD"] != null)
     {
     txtUserName.Text = Request.Cookies["USERNAME"].ToString();
    txtPassword.Text = Request.Cookies["PASSWORD"].ToString();
    }
    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.

    Comment

    Working...