Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 8th, 2003, 01:36 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Location: Smyrna, Georgia, USA.
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?
  #2 (permalink)  
Old November 8th, 2003, 02:38 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:06 PM.


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