ASP.NET
  Home arrow ASP.NET arrow Page 9 - 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 - How it Works – Setting a Cookie Using ASP–ADO


    (Page 9 of 13 )

    The first page starts by informing the user that we need to set the cookie.

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

    Continuing on the first page, we set up the form with a single text input field named PeopleID. The Submit button will direct the browser to pull up the CookieSetter page.

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

    The second page takes the data and uses it to set a cookie. Of course, we want our cookie to persist beyond this session, so we set an expiry date.

    <%@ Language=VBScript %>
    <%Response.Buffer=true%>

    <TITLE>2726-11-TI-O2 Cookies With ADO CookieSetter</TITLE>
    </HEAD><BODY>
    <%
      varPeopleID = Request.Form("PeopleID")
    '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>

    Now, in the home page, we can use ADO to use the cookie to look up a specific person's names and yacht club code. We start with the usual housekeeping, and then read the cookie from the Request object into a variable:

    <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")

    Then we create a SQL statement that has the cookie data in the WHERE clause, as in the lines below. Note that I have written a space both after People and before WHERE. That puts two spaces in the SQL statement when only one is needed. One of the most common student mistakes occurs when a space is left out of that position. I suggest that when students are building multi-line SQL statements they put a space both at the end of the FROM clause and the beginning of the WHERE clause. If, in the course of editing, one is removed, the statement will still have the other. SQL will never fail because of the extra space, so "better spaced than sorry".

      Set oRS=Server.CreateObject("ADODB.Recordset")
      sqltxt = "SELECT PeopleNameFirst, PeopleNameLast, PeopleClubCode"
      sqltxt = sqltxt & " FROM People "
      sqltxt = sqltxt & " WHERE PeopleID = " & varPeopleIDNumber & ";"

    Finally, we use that SQL statement to open and read from a record set, as follows:

      oRS.Open sqltxt, "DSN=sailors"
      Response.Write oRS("PeopleNameFirst") & " "
      Response.Write oRS("PeopleNameLast")
    %>
    </H1>
    Your yacht club code is <%=oRS("PeopleClubCode")%>
    </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

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





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