ASP.NET
  Home arrow ASP.NET arrow Page 4 - Databases and Cookies
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Databases and Cookies
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2004-11-03

    Table of Contents:
  • Databases and Cookies
  • Key Points About Cookies
  • Looking at your cookies
  • Examples of Using Cookies
  • Try It Out – Setting and Reading a Cookie with ASP Alone
  • How it Works – Setting and Reading a Cookie with ASP Alone
  • Using Cookies with ADO and a Database
  • Try It Out – Setting a Cookie Using ASP–ADO
  • How it Works – Setting a Cookie Using ASP–ADO
  • Resetting a Cookie
  • Try It Out – Resetting a Cookie
  • How It Works – Resetting a Cookie
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Databases and Cookies - Examples of Using Cookies


    (Page 4 of 13 )

    Let's look at several levels of using cookies, the simplest requiring ASP only (no ADO). A simple case would be to store in a cookie a user's preference about how to display a page. The technique would work like this for a site called www.MyCookieSite.com:

    • During the first visit to www.MyCookieSite.com the site uses a form page to ask the user if they prefer red or black color characters.

    • That preference is set in a cookie by code contained in a second page that uses ASP.

    • Subsequently when that user logs in to www.MyCookieSite.com, the browser sends along the cookie to the server. The ASP code checks the preference in the cookie and sets the character color to suit the preference. The visitor sees the page with the color he selected.

    Now let's take a more sophisticated case where we use the cookie to store a membership number, and then use ADO to look that up in a database and derive additional information about the member.

    • When the visitor joins www.MyClub.org, the site uses a form page to gather from the user their mailing address, including the zip code. The new member is added to the database and assigned a member number.

    • That member number is set on the user's hard drive in a cookie, by code contained in a second page.

    • The next time that the member logs in to www.MyClub.org, the browser sends along the member number in the cookie to the server. The ASP/ADO code uses that number in the WHERE clause of a SQL statement and gets the member's zip code. Based on that zip code, ASP builds the rest of the page to feature events in the area of the member.
    Syntax to Work with Cookies in Simple ASP (no ADO)

    Prior to running through the techniques of using cookies with databases via ADO, let us briefly look at how to use cookies with just ASP. Again, if you are already using these types of commands, skip to the section titled "Using Cookies with ADO and a Database".

    Syntax to Set (Write) Cookies in Simple ASP (no ADO)

    These techniques are explained in more depth in Beginning Active Server Pages (ISBN 1-861001-34-7) and Professional Active Server Pages (ISBN 1-861001-26-6), but I will run through them here. To set a persistent cookie you need three lines within ASP delimiters. The first line must go at the very beginning of the page, even before the<HTML> tag:

    <%Response.Buffer=true%>
    <HTML>

    <BODY>
    <%
      Response.Cookies("FirstCookie")="DataInCookie"
      Response.Cookies("FirstCookie").Expires = date + 365
    %>

    The first line is the trickiest. Normally ASP starts writing a header and then the HTML of the page as it is interpreting the ASP code. But the instructions to set a cookie must go into the header and if that header is already written then the cookie-setting commands will fail. So we must use <%Response.Buffer=true%> to tell ASP to wait on writing the page, because later in the page we want to write code into the header.

    The first line of ASP code in the body sets the value of the data and the name of the cookie. However, cookies by default do not persist beyond the time the browser is open so we must specifically set an expiration date on the following line. In this case we pick up today's date, add a year's worth of days, and use that for the expiration date.

    Syntax to Read Cookies in Simple ASP (no ADO)

    To read a cookie into a variable you need one line to get the cookie's data out of the Request object and into a variable. You can then use that variable as needed. There is no need to worry about buffering since you are not adding characters to the header.

    <%
      Dim varCookie
      varCookie = Request.Cookies("FirstCookie")
      Response.Write varCookie
    %>

    In the above code we start by declaring the variable, then filling it. A following line writes the contents of the variable to the page. Reading a cookie does not require any buffering of the response, only setting a cookie requires page buffering.

    Many students ask why the cookie data is first copied into a variable rather than used directly out of the Request object in a statement like the following:

    Response.Write Request.Cookies("FirstCookie")

    There is a rumor that Microsoft does not guarantee being able to extract cookie data from a request string more than once, however, if the data is extracted into a variable then the variable is, of course, available for the entire page. Although many programmers use the data directly without problems, I tend to take the more cautious approach. Don't be confused by this caveat. The cookie itself remains re-usable on the browser's hard disk. It is the copy of the cookie that was sent in the request that cannot be guaranteed for re-use. A subsequent request, with its new copy of the cookie, will be readable.

    Syntax for Cookies with Multiple Data with Simple ASP (no ADO)

    There is one last topic before getting into cookies with ADO. Sometimes we want to hold more than one datum in a cookie. This is done by using keys, which are like subfolders within a cookie. Each key has a name and a value.

    With multiple keys the code is as follows:

    <%Response.Buffer=true%>
    <HTML>

    <BODY>
    <%
      Response.Cookies("SecondCookie")("FirstDatum")="FirstDataInSecondCookie"
      Response.Cookies("SecondCookie")("SecondDatum")="SecondDataInSecondCookie"
      Response.Cookies("SecondCookie")("ThirdDatum")="ThirdDataInSecondCookie"
      Response.Cookies("SecondCookie").Expires = date + 365
    %>

    We can retrieve the data from multiple keys within a cookie with the following code:

    <%
      Dim varSecondCookie1
      Dim varSecondCookie2
      Dim varSecondCookie3
      varSecondCookie1 = Request.Cookies("SecondCookie")("FirstDatum")
      varSecondCookie2 = Request.Cookies("SecondCookie")("SecondDatum")
      varSecondCookie3 = Request.Cookies("SecondCookie")("ThirdDatum")
      Response.Write varSecondCookie1 & "<BR>"
      Response.Write varSecondCookie2 & "<BR>"
      Response.Write varSecondCookie3 & "<BR>"
    %>

    Notice in the above code that we follow the name of the cookie with a key name. Also note that expiration is set for the entire cookie, you cannot set different expirations for each key within a cookie. If you need to have different expirations then you must use different cookies, not different keys within one cookie.  

    This is from Beginning ASP Databases by Kauffman, Spencer, and Willis (Apress, ISBN 1590592492). Check it out at your favorite bookstore today.

    Buy this book now.

    More ASP.NET Articles
    More By Apress Publishing


       · i really need help adding the updated information to my database
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek