Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | 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 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 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!
  #2 (permalink)  
Old March 29th, 2004, 02:55 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
  #3 (permalink)  
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>



  #4 (permalink)  
Old March 30th, 2004, 04:03 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
 


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
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



All times are GMT -4. The time now is 02:40 AM.


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