Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 8th, 2003, 01:36 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cookies

I'm attempting to learn how to use cookies, but I'm getting some inconsistent results. After I instantiate and populate the cookies in the first page with the response object, I'm able to access those cookies from another page with the request object once.

After I close the browser, if I try to open the browser again and go to the page that accesses these cookies, I get an error message that references the line with the first Request.Cookie object and gives the following error message: Object reference not set to an instance of an object.

Here's the first page:

<%@ Page Language="c#" %>
<script language="c#" runat="server">
void SubmitInfo()
{
    HttpCookie ckeName = new HttpCookie("Name");
    ckeName.Value = txtName.Text;
    Response.Cookies.Add(ckeName);

    HttpCookie ckeStreetAddress = new HttpCookie("StreetAddress");
    ckeStreetAddress.Value = txtStreet.Text;
    Response.Cookies.Add(ckeStreetAddress);

    HttpCookie ckeCityStateZip = new HttpCookie("CityStateZip");
    ckeCityStateZip.Value = txtCityStateZip.Text;
    Response.Cookies.Add(ckeCityStateZip);
}
</script>

<html>
<body>
<form id="frmUserInfo" runat="server">
Name: <asp:textbox id="txtName" runat="server" />
<br>
Street Address: <asp:textbox id="txtStreet" runat="server" />
<br>
City, State Zip: <asp:textbox id="txtCityStateZip" runat="server" />
<br>
<br>
<asp:button id="btnSubmit" text="Submit Form" runat="server" onclick="SubmitInfo" />
</form>
</body>
</html>

Here's the second page that's generating the error message, referenced above, on the first line using my Request object:

<%@ Page Language="c#" %>
<script language="c#" runat="server">
void Page_Load(object Source, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now);

    Response.Write("You entered the following information: <br><br>");

    Response.Write(Request.Cookies["Name"].Value + "<br>");
    Response.Write(Request.Cookies["StreetAddress"].Value + "<br>");
    Response.Write(Request.Cookies["CityStateZip"].Value + "<br>");
}

</script>

<html>
<body>
</body>
</html>

Can anybody bring me up to speed on what I need to do differently?
 
Old November 8th, 2003, 02:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

By default, cookies expire when you close the browser. That is, a cookie exists only for the duration of a browser session.

To make cookies durable, you need to set the Expires property, like this:
Code:
HttpCookie ckeName = new HttpCookie("Name");
ckeName.Value = txtName.Text;
ckeName.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(ckeName);
This makes the cookie last for the next 10 days.

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookies hastikeyvan Classic ASP Professional 5 May 29th, 2006 07:36 AM
Should I use cookies?? Help Tee88 Classic ASP Basics 3 January 21st, 2005 12:33 AM
Cookies shabboleth Beginning PHP 3 December 30th, 2003 10:32 AM
Help!About Cookies jelsen ASP.NET 1.0 and 1.1 Basics 2 September 9th, 2003 08:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.