ASP.NET
  Home arrow ASP.NET arrow Page 11 - 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 – Resetting a Cookie


    (Page 11 of 13 )

    Our objective is to show on the home page the visitor's name and professional class, based on the person's ID stored as a cookie. We will do that by first reviewing the home page from the last Try It Out, which does not have a cookie check. Then we will modify that home page to check for cookies. If there is no cookie, then the visitor will be shunted to ResetCookieForm.asp and that leads to ResetCookieResponse.asp.

    One note on this exercise. In the course of this exercise you will probably want to delete a cookie for testing. After deleting the cookie it is best to re-start your browser.

    You can delete a cookie by hand from IE by going to C:\Windows\cookies . It will probably be named UserName@SiteName . You can also sort by date and you will see your cookie as the most recently changed. In Netscape a cookie is located at: c:\program files\netscape\users\<profile name>\cookies.txt

    The code for a typical home page that uses a cookie would look like:

    <%@ Language=VBScript %>
    <HTML><HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726-11-TI-O3 Reset Cookie Home Page No Check</TITLE>
    </HEAD><BODY>
    <H1>Welcome to the Sailor's Home Page</H1>
    <P>(no cookie check)</P>
    <%
      Dim varPeopleID
      Dim oRS
      varID=Request.Cookies("PeopleID")
      sqltxt = "SELECT PeopleNameLast, PeopleNameFirst, "
      sqltxt = sqltxt & "PeopleProfessionalClass FROM People "
      sqltxt = sqltxt & " WHERE PeopleID =" & varID & ";"
      Set oRS = Server.CreateObject("ADODB.Recordset")
      oRS.open sqltxt, "DSN=Sailors"
      Response.Write "Welcome " & oRS("PeopleNameFirst")
      Response.Write " " & oRS("PeopleNameLast") & "<BR>"
      Response.Write "<P>We have you registered as "
      Response.Write " of Professional Class&nbsp;"
      Response.Write oRS("PeopleProfessionalClass")
    %>
    </BODY></HTML>

    And the above code (available as file 2726-11-Cook-TI-03-ResetCookieHomePageNoCheck.asp) would produce the following page:

     

    But when we consider that cookies may be unusable or unavailable we have to modify that home page to detect the absence of a cookie. If the cookie does not exist then the user is notified and asked to click to the Cookie Reset page, as follows. This code is available as file 2726-11-TI-03-ResetCookieHomePageWithCheck.asp:

    <HTML>
    <HEAD>
    <TITLE>2726-11-TI-O3 Reset Cookie Home Page With Check</TITLE>
    </HEAD>
    <BODY>
    <%
      Dim varPeopleID, oRS
      varPeopleID=Request.Cookies("PeopleID")
      If varPeopleID = "" Then
        Response.Write "<H1>This is the Sailor's Home Page, but...</H1>"
        Response.Write "<P>(failed cookie check)</P>"
        Response.Write "Sorry, we can not find your cookie<BR>"
        Response.Write "<A href='2726-11-TI-O3-ResetCookieForm.asp'>"
        Response.Write "Please click here </A>to re-set your cookie"
      Else
        Response.Write "<H1>Welcome to the Sailor's Home Page</H1>"
        Response.Write "<P>(passed cookie check)</P>"
        Set oRS = Server.CreateObject("ADODB.Recordset")
        sqltxt = "SELECT PeopleNameLast, PeopleNameFirst, "
        sqltxt = sqltxt & "PeopleProfessionalClass "
        sqltxt = sqltxt & " FROM People WHERE PeopleID=" & varPeopleID & ";"
        oRS.Open sqltxt, "DSN=Sailors"
        Response.Write "<P>We have you, " & oRS("PeopleNameFirst") & " "
        Response.Write oRS("PeopleNameLast") & ", "
        Response.Write "registered in Professional Class&nbsp;"
        Response.Write oRS("PeopleProfessionalClass") & "</P>"
      End If
    %>
    </BODY></HTML>

    The above cookie-checking page gives the same result as the last page if a cookie is present, but if the cookie is missing, then we get the following:

     

    The user would click to reset a new cookie, and would hit the following code, file 2726-11-TI-03-ResetCookieForm.asp, to allow selection of the cookie to set:

    <%@ Language=VBScript %>
    <HTML><HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726-11-TI-O3 Reset Cookie Form</TITLE>
    </HEAD><BODY>
    <H1>Reset Cookie</H1>
    <P>Sorry, but we are having problems identifying you.</P>
    <P>Please select your name from the list below</P>
    <FORM ACTION="2726-11-TI-O3-ResetCookieResponse.asp" METHOD=get>
    <SELECT NAME="PeopleName">

    <%
      Dim oRSPeople
      Set oRSPeople=Server.CreateObject("ADODB.Recordset")
      sqltxt = "Select PeopleNameLast, PeopleNameFirst from People"
      sqltxt = sqltxt & " ORDER BY PeopleNameLast;"
      oRSPeople.Open sqltxt, "DSN=Sailors"
      Do while not oRSPeople.EOF
        Response.Write "<OPTION NAME=>"
        Response.Write orsPeople("PeopleNameLast") & ", "
        Response.Write orsPeople("PeopleNameFirst") & "</OPTION>"
        oRSPeople.MoveNext
      Loop
    %>
    </SELECT>
    <INPUT TYPE="Submit">
    <INPUT TYPE="Reset">
    </FORM></BODY></HTML>

    Which produces the following page:

     

    The form needs a response, which in this case actually sets the cookie, and we would confirm that by informing the user of setting a cookie and giving her a hyperlink back to the home page, where she can use the new cookie. The code for this page is taken from 2726-11-TI-03-ResetCookieResponse.asp and is listed below:

    <%@ Language=VBScript%>
    <%Response.Buffer = true%>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726-11-TI-O3 Reset Cookie Response</TITLE>
    </HEAD>
    <BODY>

    <P>&nbsp;</P>
    <H1>This page will re-set your cookie </H1>
    <%
      varPeopleNameTotal = Request.Querystring("PeopleName")
      varPeopleNameLast = left(varPeopleNameTotal,instr
       (varPeopleNameTotal, ",")-1)
      varPeopleNameFirst = right(varPeopleNameTotal,len
       (varPeopleNameTotal)- instr(varPeopleNameTotal, ",")-1)
      Dim oRSPeopleIDLookUp
      Set oRSPeopleIDLookUp=Server.CreateObject
       ("ADODB.Recordset")
      sqltxt = "Select PeopleID from People"
      sqltxt = sqltxt & " WHERE PeopleNameLast = '" &
        varpeoplenamelast & "'"
      sqltxt = sqltxt & " AND PeopleNameFirst = '" &
        varpeoplenamefirst & "';"
      oRSPeopleIDLookUp.Open sqltxt, "DSN=Sailors"
      Response.Cookies("PeopleID") = oRSPeopleIDLookUp
        ("PeopleID")
      Response.Cookies("PeopleID").Expires = date + 365
    %>
    We have re-set your cookie, the identification is now for:<BR><BR>
    <%=varPeopleNameFirst%>&nbsp;<%=varPeopleNameLast%>
    with the ID number <%=oRSPeopleIDLookUp("PeopleID")%>
    <BR><BR>Click here to re-try
    <A HREF="2726-11-TI-O3-ResetCookieHomePageWithCheck.asp">
    homepage</A>
    with your new cookie
    </BODY>
    </HTML>

    Which gives the user the following page, reporting back to the user that the cookie has been set.

     

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