Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 1.0
This is the forum to discuss the Wrox book Beginning ASP.NET 1.0 with C# by Chris Goode, John Kauffman, Christopher L. Miller, Neil Raybould, S. Srinivasa Sivakumar, Dave Sussman, Ollie Cornes, Rob Birdwell, Matt Butler, Gary Johnson, Ajoy Krishnamoorthy, Juan T. Llibre, Chris Ullman; ISBN: 9780764543708
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 1.0 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 March 29th, 2004, 01:51 AM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 10, Cookies

Hi,

After I run the cookie1.aspx file, and I can't find the background cookie file anywhere in my harddrive.

Also, I closed my browser after I ran the cookies1.aspx file and set the background color to red and closed my browser and I got an error when I try to run cookie2.aspx. Looks like the background cookie didn't stored to my cookies' folder, and when cookie2.aspx try to reference it, it generated an errors saying can't reference a null object. Please help anyone.

Thanks!
 
Old March 29th, 2004, 02:55 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Can you please post the code? Not everyone has the book, so it's hard to see where the problem is without seeing some of the code.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 29th, 2004, 03:12 PM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for your help! here are the code for both cookie1.aspx and cookie2.aspx.

Cookie1.aspx

<%@ Page Language="c#" %>
<script Language="c#" runat="server">
  void Page_Load(object source, EventArgs e)
  {
    if (!(IsPostBack))
    {
      MyButton.Text = "Save Cookie";
      MyDropDownList.Items.Add("Blue");
      MyDropDownList.Items.Add("Red");
      MyDropDownList.Items.Add("Gray");
    }
  }
  public void Click(object sender, EventArgs e)
  {
    HttpCookie MyCookie = new HttpCookie("Background");
    MyCookie.Value = MyDropDownList.SelectedItem.Text;
    Response.Cookies.Add(MyCookie);
  }

</script>

<html>
  <body>
    <form id="CookieForm" method="post" runat="server">
      <asp:DropDownList id=MyDropDownList runat="server"/>
      <asp:button id=MyButton runat="server" OnClick="Click"/>
    </form>
  </body>
</html>


Cookie2.aspx

<%@ Page Language="c#" %>
  <script Language="c#" runat="server">
    void Page_Load(object source, EventArgs e)
    {
      Response.Cache.SetExpires(DateTime.Now);
    }
    string GetBackground()
    {
      return Request.Cookies["Background"].Value;
    }
  </script>
  <html>
    <body bgcolor="<% Response.Write(GetBackground()); %>">
    <asp:label id="message" runat="server" />
    </body>
  </html>



 
Old March 30th, 2004, 04:03 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It looks like you haven't set the Expires property of the cookie. If you don't provide a date to this property, the cookie will only last for the current browser session (it's not saved to disk, and deleted when you close the last browser window).

On Cookie1.aspx, make the cookie last 30 minutes with this code (from the MSDN page I linked to in this post):
Code:
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 30, 0);

HttpCookie MyCookie = new HttpCookie("Background");

MyCookie.Value = MyDropDownList.SelectedItem.Text;
MyCookie.Expires = dt.Add(ts);

Response.Cookies.Add(MyCookie);
This way, even if you close the browser, the cookie will last for 30 minutes (by saving it to disk).

You should also never assume that a cookie exists. That means that on Cookie2,aspx, you should find out if the cookie is null or not. If it isn't, retrieve and use its value, otherwise make up a (default)value, or skip the code entirely:
Code:
string GetBackground()
{
  if (Request.Cookies["Background"] != null)
  {
    return Request.Cookies["Background"].Value;
  }
  else
  {
    // Cookie does not exist. Return a default value
    return "#ffffff";
  }
}
This way, even if the cookie doesn't exist, you won't get an error, but you get the default background color instead.

Does this help?

Imar


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




Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10 gogeo BOOK: Beginning Access 2003 VBA 1 January 22nd, 2006 09:41 AM
Chapter 10 columbiasmiles JSP Basics 0 May 17th, 2004 08:09 PM
Chapter 10 -- Using Cookies Michael Randall BOOK: Beginning ASP.NET 1.0 2 January 25th, 2004 05:17 PM





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