ASP.NET
  Home arrow ASP.NET arrow Page 8 - 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 - Try It Out – Setting a Cookie Using ASP–ADO


    (Page 8 of 13 )

    Our objective is to set a cookie with the user's ID number, then use that to look up the visitor's yacht club code in the database, and display that information on the home page. To make this example simpler, we will assume the visitors know their membership number from their membership card.

    Firstly, let us look at the overall strategy that we will use. The four steps we will follow are:

    • Get the visitor's membership number on a form.

    • Second, we set a cookie that holds the visitor's ID number from the People table.

    • Third we can use the ID from the cookie to find their record in the table.

    • Lastly, from the correct record we can show the person's yacht club code.

    This will require three pages. The first is a CookieSetterForm page that gathers the visitor's ID. The second page, CookieSetter, actually sets the cookie and confirms that act to the user. The third page, CookieHome, uses the emerging cookie and ADO to derive the name of the visitor's yacht club code.

    The first page, in file 2726-11-Cook-TIO-02-CookiesWithADOCookieSetterForm.asp follows:

    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726-11-TI-O2 Cookies With ADO CookieSetterForm</TITLE>
    </HEAD>
    <BODY>

    <H1>Welcome to the Sailors Site, But First...</H1>
    <P>Please provide your membership number so we can set your cookie.</P>
    <FORM ACTION="2726-11-Cook-TIO-02-CookiesWithADOCookieSetter.asp" METHOD=post>
    Please type your membership number here
    <INPUT NAME='PeopleID'>
    <INPUT TYPE=submit VALUE="Submit">
    </FORM>
    </BODY>
    </HTML>

    Which gives this result:

     

    The second page, 2726-11-Cook-TIO-02-CookiesWithADOCookieSetter.asp, takes the data and uses it to set a cookie, as follows:

    <%@ Language=VBScript %>
    <%Response.Buffer=true%>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726-11-TI-O2 Cookies With ADO CookieSetter</TITLE>
    </HEAD>
    <BODY>

    <%
      varPeopleID = Request.Form("PeopleID")
    'next line for testing
    'Response.Write varpeopleID
      Response.Cookies("PeopleIDNumber")=varPeopleID
      Response.Cookies("PeopleIDNumber").Expires = Date + 366
    %>
    A cookie has been set with your membership ID number of <%=varPeopleID%><BR>
    <A HREF="2726-11-Cook-TIO-O2-CookiesWithADOCookieHome.asp">
    Click here to return to the home page</A>
    </BODY>
    </HTML>

    The above code gives the following result:

     

    Then we finish by clicking back to the home page, whose code is in file 2726-11-Cook-TIO-02-CookiesWithADOCookieHome.asp and below:

    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726-11-TI-O2 Cookies With ADO Home</TITLE>
    </HEAD>
    <BODY>
    <H1>Welcome to the Sailor's Home Site <BR><BR>
    <%
      Dim oRS, varPeopleIDNumber
      varPeopleIDNumber = Request.Cookies("PeopleIDNumber")
      Set oRS=Server.CreateObject("ADODB.Recordset")
      sqltxt = "SELECT PeopleNameFirst, PeopleNameLast, 
        PeopleClubCode"
      sqltxt = sqltxt & " FROM People "
      sqltxt = sqltxt & " WHERE PeopleID = " &
        varPeopleIDNumber & ";"
      oRS.Open sqltxt, "DSN=sailors"
      Response.Write oRS("PeopleNameFirst") & " "
      Response.Write oRS("PeopleNameLast")
    %>
    </H1>
    Your yacht club code is <%=oRS("PeopleClubCode")%>
    </BODY>
    </HTML>

    The above code produces the following screen:

     

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek