locked
cookies help c# and asp RRS feed

  • Question

  • User532117409 posted

    hello i am new in progrmaing i want to use cookies what are they and how to use them i am making a simple student info system using
    c#,asp,sql i have use session variable for displaying the user login name and application variable for the sql connection string
    now plz help me with a simple use of cookies and their syntax and how can i use them in this application

    thanks in advance

    Monday, February 11, 2013 5:04 AM

Answers

  • User1929035129 posted

     protected void Page_Load(object sender, System.EventArgs e) {
            HttpCookie userCookie = new HttpCookie("UserInfo");
            userCookie["Country"] = "Italy";
            userCookie["City"] = "Rome";
            userCookie["Name"] = "Jones";
            userCookie.Expires = DateTime.Now.AddDays(3);
            Response.Cookies.Add(userCookie);
            Label1.Text = "Cookie create successful!<br><br/>";
            
            HttpCookie cookie = Request.Cookies["UserInfo"];
            
            if (cookie != null)
            {
                string country = cookie["Country"];
                string city = cookie["City"];
                string name = cookie["Name"];
                Label2.Text = "Cookie Found and read<br/>";
                Label2.Text += "Name: " + name;
                Label2.Text += "<br />Country: " + country;
                Label2.Text += "<br />City: " + city;
            }
        }

    http://asp-net-example.blogspot.in/2009/01/aspnet-cookie-example-how-to-create.html


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2013 5:34 AM

All replies

  • User-837620913 posted

    You can use cookies to store small bits of information. The max size of a cookie is 4096 bytes.  From the documentation:

    For example, if a user requests a page from your site and your application sends not just a page, but also a cookie containing the date and time, when the user's browser gets the page, the browser also gets the cookie, which it stores in a folder on the user's hard disk.

    Later, if user requests a page from your site again, when the user enters the URL the browser looks on the local hard disk for a cookie associated with the URL. If the cookie exists, the browser sends the cookie to your site along with the page request. Your application can then determine the date and time that the user last visited the site. You might use the information to display a message to the user or check an expiration date.

    Here is a quick example on how to set cookies:

    Response.Cookies["userName"].Value = "patrick";
    Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
    
    HttpCookie aCookie = new HttpCookie("lastVisit");
    aCookie.Value = DateTime.Now.ToString();
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);

    Find out more here:  http://msdn.microsoft.com/en-us/library/ms178194(v=VS.100).aspx

    Monday, February 11, 2013 5:08 AM
  • User-2143599517 posted

    A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.

    http://msdn.microsoft.com/en-us/library/ms178194%28v=vs.100%29.aspx

    http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies

    Monday, February 11, 2013 5:28 AM
  • User1929035129 posted

     protected void Page_Load(object sender, System.EventArgs e) {
            HttpCookie userCookie = new HttpCookie("UserInfo");
            userCookie["Country"] = "Italy";
            userCookie["City"] = "Rome";
            userCookie["Name"] = "Jones";
            userCookie.Expires = DateTime.Now.AddDays(3);
            Response.Cookies.Add(userCookie);
            Label1.Text = "Cookie create successful!<br><br/>";
            
            HttpCookie cookie = Request.Cookies["UserInfo"];
            
            if (cookie != null)
            {
                string country = cookie["Country"];
                string city = cookie["City"];
                string name = cookie["Name"];
                Label2.Text = "Cookie Found and read<br/>";
                Label2.Text += "Name: " + name;
                Label2.Text += "<br />Country: " + country;
                Label2.Text += "<br />City: " + city;
            }
        }

    http://asp-net-example.blogspot.in/2009/01/aspnet-cookie-example-how-to-create.html


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2013 5:34 AM
  • User-1716253493 posted
    cookies is variable saved in your browser. You can read it before the cookies expired or you delete the browser cookies.
    Monday, February 11, 2013 6:11 AM
  • User532117409 posted

    there is one problem which i am facing in my menupage.aspx i have add a label i can use this in menupage.aspx.cs but now when i am editing this page i am adding more lables but in aspx.cs file the error is not in this context i am writing the correct label name what can be the cause ?

    Monday, February 11, 2013 6:47 AM
  • User-1716253493 posted

    there is one problem which i am facing in my menupage.aspx i have add a label i can use this in menupage.aspx.cs but now when i am editing this page i am adding more lables but in aspx.cs file the error is not in this context i am writing the correct label name what can be the cause ?

    If you ask defent question better create new threath. If any answer above help you then close this threat by click masrk as answer

    Monday, February 11, 2013 8:54 AM