ASP.NET
  Home arrow ASP.NET arrow Page 12 - 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 - How It Works – Resetting a Cookie


    (Page 12 of 13 )

    The home page in its original form could not handle visitors without a cookie. The following code would put a blank into varPeopleID. That causes a logical failure, but not an ASP error since VBScript accepts putting "nothing" into a variable.

    <TITLE>2726-11-TI-O3 Reset Cookie Home Page No Check</TITLE>

    varID=Request.Cookies("PeopleID")

    However, the following code would fail because SQL will not accept a null value for the expression of the WHERE clause.

    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"

    So we use the code of the improved page, which has an If to determine if the cookie is empty:

    <TITLE>2726-11-TI-O3 Reset Cookie Home Page With Check</TITLE>

    Dim varPeopleID, oRS
    varPeopleID=Request.Cookies("PeopleID")
    If varPeopleID = "" Then

    In the following code we determine if the cookie is empty, and if so give the "Sorry but … " message with a
    hyperlink to a form to reset the cookie (2726–11–TI–O3-ResetCookieForm.asp). After getting this to
    work, you may consider changing the code to automatically redirect the user using Response.Buffer=true and Response.Redirect. However, I find that students understand the concepts better on their first try by clicking through the pages.

      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 reset your cookie"

    If the cookie exists we perform the following code to get the name and professional class of the visitor from the database. Note that we use the cookie as the expression of the WHERE clause in the SQL
    statement. Since the PersonID is a number, we do not concatenate quotes around the variable in the last line below:

    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 & ";"

    Then we can write the text of the home page using data from the recordset:

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

    But let us get back to the path of visitors without a cookie. They got the "Sorry but…" message on the home page and click to come to 2726–11–TI–O3-ResetCookieForm.asp. Our objective is to set up a
    list of the available names from which to choose. The first part of that code, below, creates a form and a list box using the <SELECT> tag.

    <TITLE>2726-11-TI-O3 Reset Cookie Form</TITLE>

    <FORM ACTION="2726-11-TI-O3-ResetCookieResponse.asp" METHOD=get>
    <SELECT NAME="PeopleName">

    Then we create a recordset of the members so we can put them into a list box:

    <%
      Dim oRSPeople
      Set oRSPeople=Server.CreateObject("ADODB.Recordset")
      sqltxt = "Select PeopleNameLast, PeopleNameFirst from People"
      sqltxt = sqltxt & " ORDER BY PeopleNameLast;"
      oRSPeople.Open sqltxt, "DSN=Sailors"

    Then we actually populate the list box by looping through that recordset creating an <OPTION> tag for each member. As always, be sure to include the RS.MoveNext in the loop.

      Do while not oRSPeople.EOF
        Response.Write "<OPTION NAME= >"
        Response.Write orsPeople("PeopleNameLast") & ", "
        Response.Write orsPeople("PeopleNameFirst") & "</OPTION>"
        oRSPeople.MoveNext
      Loop
    %>
    </SELECT>

    After the user has made a selection, the Action of the FORM tag rolls us to 2726–11–TI–O3-ResetCookieResponse.asp. Since we will be setting a cookie we must start by turning on the response
    buffer:

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

    Our incoming data is the name of the member selected from the list box, in the form of last name, and comma, and the first name. We need to not only harvest that data from the querystring, but also parse apart the first and last names so we can compare the names with the database and extract the person ID.
    Our clue to separating the names is the comma. The third line below puts into the last name variable all of the text to the left of the comma:

    <%
      varPeopleNameTotal = Request.Querystring("PeopleName")
      varPeopleNameLast = left(varPeopleNameTotal,instr(varPeopleNameTotal, ",")-1)

    The line below puts the first name into a variable:

    varPeopleNameFirst = right(varPeopleNameTotal,len(varPeopleNameTotal)-instr(varPeopleNameTotal, ",")-1)

    Now we can create a recordset that selects out the one record matching the first and last names. Note that we are applying two tests in the WHERE clause; one for the last name and one for the first name.

    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"

    We can now use the PeopleID data, from the one correct record sitting in the recordset, to write a cookie.

      Response.Cookies("PeopleID") = oRSPeopleIDLookUp("PeopleID")
      Response.Cookies("PeopleID").Expires = date + 365
    %>

    Lastly, we wrap up with some notices to the user about what we have done:

    We have reset 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>

    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 5 hosted by Hostway
    Stay green...Green IT