locked
Working with cookies in C# RRS feed

  • Question

  • User1953081067 posted

    Hi

    I've got the following code working for 1 textbox value using 1 cookie, but I'd like to store two values in the same cookie.

        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox TextBox_ClubID = (TextBox)Login_TheOffice.FindControl("TextBox_ClubID");
            TextBox TextBox_Username = (TextBox)Login_TheOffice.FindControl("Username");
    
            if (!Page.IsPostBack)
            {
                if (Request.Cookies["clubID"] != null)
                {
                    TextBox_ClubID.Text = Request.Cookies["clubID"].Value;
                }
                else
                {
                    HttpCookie MyCookie = new HttpCookie("clubID", TextBox_ClubID.Text);
                    DateTime now = DateTime.Now;
    
                    MyCookie.Value = TextBox_ClubID.Text;
                    MyCookie.Expires = now.AddHours(1);
    
                    Response.Cookies.Add(MyCookie);
                }
            }
        }



    Any ideas?

    Thanks as always

    Tuesday, January 1, 2013 8:37 AM

Answers

  • User-861818263 posted
      protected void Page_Load(object sender, EventArgs e)
            {
                TextBox TextBox_ClubID = (TextBox)Login_TheOffice.FindControl("TextBox_ClubID");
                TextBox TextBox_Username = (TextBox)Login_TheOffice.FindControl("Username");
    
                if (!Page.IsPostBack)
                {
                    if (Request.Cookies["ClubEngineLogin"] != null)
                    {
                        string sClubID = string.Empty;
                        string sUsername = string.Empty;
                        HttpCookie ClubEngineLogin = Request.Cookies["ClubEngineLogin"];
                        sClubID = ClubEngineLogin["ClubID"].ToString();
                        sUsername = ClubEngineLogin["Username"].ToString();
    
                        TextBox_ClubID.Text = sClubID;
                        TextBox_Username.Text = sUsername;
                    }
                    else
                    {
                        HttpCookie ClubEngineLogin = new HttpCookie("ClubEngineLogin");
                        DateTime now = DateTime.Now;
    
                        ClubEngineLogin["ClubID"] = TextBox_ClubID.Text;
                        ClubEngineLogin["Username"] = TextBox_Username.Text;
                        ClubEngineLogin.Expires = now.AddHours(1);
    
                        Response.Cookies.Add(ClubEngineLogin);
                    }
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 2, 2013 2:35 AM

All replies

  • User-1716253493 posted

    try

            string[] str = { TextBox1.Text,TextBox2.Text };
            Request.Cookies["clubID"] = str;
    


    or

     HttpCookie MyCookie = new HttpCookie("clubID", str);



    Tuesday, January 1, 2013 8:44 AM
  • User1256627138 posted

    Hi,

             If you want to keep both the values in same cookie why dont you try by concatenating the

    values by delimiter (i.e using special characters) then split it while displaying.

    Tuesday, January 1, 2013 8:47 AM
  • User-266170941 posted

    How to create a cookie? 
      
    Its really easy to create a cookie in the Asp.Net with help of Response object or HttpCookie 
      
    Example 1
      
            HttpCookie userInfo = new HttpCookie("userInfo");
            userInfo["UserName"] = txtbox1.Text;
            userInfo["UserColor"] = txtbox2.Text;
            userInfo.Expires.Add(new TimeSpan(0, 1, 0));
            Response.Cookies.Add(userInfo);

    Example 2
      
            Response.Cookies["userName"].Value = txtbox1.Text;
            Response.Cookies["userColor"].Value = txtbox2.Text;
                            
    How to retrieve from cookie? 

    Its easy way to retrieve cookie value form cookes by help of Request object. 
      
    Example 1
      
            string User_Name = string.Empty;
            string User_Color = string.Empty;
            User_Name = Request.Cookies["userName"].Value;
            User_Color = Request.Cookies["userColor"].Value;

    Example 2
      
            string User_name = string.Empty;
            string User_color = string.Empty;
            HttpCookie reqCookies = Request.Cookies["userInfo"];
            if (reqCookies != null)
            {
                User_name = reqCookies["UserName"].ToString();
                User_color = reqCookies["UserColor"].ToString();
            }


    More details,please refer these links

    http://www.c-sharpcorner.com/uploadfile/annathurai/cookies-in-Asp-Net/

    http://www.mini.pw.edu.pl/~mossakow/materials/presentations/aspnet.3.5/state_tracing/index.html

    Tuesday, January 1, 2013 8:53 AM
  • User840877147 posted
    Response.Cookies["cookie_name"]["key_1"] = "value_1";
    Response.Cookies["cookie_name"]["key_2"] = "value_2";
    Response.Cookies["cookie_name"]["key_3"] = "value_3";

    Tuesday, January 1, 2013 9:03 AM
  • User1953081067 posted

    I've changed it as suggested, but I get 'Object reference not set' at the line where I call a value:

        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox TextBox_ClubID = (TextBox)Login_TheOffice.FindControl("TextBox_ClubID");
            TextBox TextBox_Username = (TextBox)Login_TheOffice.FindControl("Username");
    
            if (!Page.IsPostBack)
            {
                if (Request.Cookies["ClubEngineLogin"] != null)
                {
                    string sClubID = string.Empty;
                    string sUsername = string.Empty;
    
                    sClubID = Request.Cookies["ClubID"].Value;
                    sUsername = Request.Cookies["Username"].Value;
    
                    TextBox_ClubID.Text = sClubID;
                    TextBox_Username.Text = sUsername;
                }
                else
                {
                    HttpCookie ClubEngineLogin = new HttpCookie("ClubEngineLogin");
                    DateTime now = DateTime.Now;
    
                    ClubEngineLogin["ClubID"] = TextBox_ClubID.Text;
                    ClubEngineLogin["Username"] = TextBox_Username.Text;
                    ClubEngineLogin.Expires = now.AddHours(1);
    
                    Response.Cookies.Add(ClubEngineLogin);
                }
            }
        }

    Any ideas?

    Tuesday, January 1, 2013 10:20 AM
  • User-266170941 posted

    Change and try this

    if (Request.Cookies["ClubEngineLogin"].ToString() =="" || Request.Cookies["ClubEngineLogin"].ToString() == null)
                {
                    HttpCookie ClubEngineLogin = new HttpCookie("ClubEngineLogin");
                    DateTime now = DateTime.Now;
                    ClubEngineLogin["ClubID"] = TextBox_ClubID.Text;
                    ClubEngineLogin["Username"] = TextBox_Username.Text;
                    ClubEngineLogin.Expires = now.AddHours(1);
                    Response.Cookies.Add(ClubEngineLogin);
                }
                else
                {
                   string sClubID = string.Empty;
                    string sUsername = string.Empty;
    
                    sClubID = Request.Cookies["ClubID"].Value;
                    sUsername = Request.Cookies["Username"].Value;
    
                    TextBox_ClubID.Text = sClubID;
                    TextBox_Username.Text = sUsername;
                }
            }
    


    Tuesday, January 1, 2013 10:38 AM
  • User-861818263 posted
      protected void Page_Load(object sender, EventArgs e)
            {
                TextBox TextBox_ClubID = (TextBox)Login_TheOffice.FindControl("TextBox_ClubID");
                TextBox TextBox_Username = (TextBox)Login_TheOffice.FindControl("Username");
    
                if (!Page.IsPostBack)
                {
                    if (Request.Cookies["ClubEngineLogin"] != null)
                    {
                        string sClubID = string.Empty;
                        string sUsername = string.Empty;
                        HttpCookie ClubEngineLogin = Request.Cookies["ClubEngineLogin"];
                        sClubID = ClubEngineLogin["ClubID"].ToString();
                        sUsername = ClubEngineLogin["Username"].ToString();
    
                        TextBox_ClubID.Text = sClubID;
                        TextBox_Username.Text = sUsername;
                    }
                    else
                    {
                        HttpCookie ClubEngineLogin = new HttpCookie("ClubEngineLogin");
                        DateTime now = DateTime.Now;
    
                        ClubEngineLogin["ClubID"] = TextBox_ClubID.Text;
                        ClubEngineLogin["Username"] = TextBox_Username.Text;
                        ClubEngineLogin.Expires = now.AddHours(1);
    
                        Response.Cookies.Add(ClubEngineLogin);
                    }
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 2, 2013 2:35 AM