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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT