ASP.NET
  Home arrow ASP.NET arrow Page 7 - 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 - Using Cookies with ADO and a Database


    (Page 7 of 13 )

    Now that we have reviewed using cookies with ASP we can see how to enhance those techniques with ASP–ADO. The syntax is direct, but students can be confused by how to handle the information across multiple pages. Setting a cookie and making a new record in a database is generally done in three pages.

    The first page gathers information in a form, this page requires neither ASP nor ADO, just simple HTML forms. That information is passed to an ASP/ADO response page, which performs two jobs. It sets the cookie on the user's PC, and writes the information to the database. Since the user sees neither of those processes, I usually include some text that repeats the data back to the user, and notes that a cookie has been set and they have been entered in the database, along with a hyperlink button to request the third page.

    As a robust test we then write a third page to test our process, usually by reading the cookie and using it to look up the visitor's information. I usually add a button here that allows the user to catch a mistake. That button takes the visitor to an editing screen to correct the errors.

    Reading Cookies for Use in Database Look-Up

    Once a cookie is set we can use it in subsequent visits to link the visitor to our database. For example, we can use the ID to find out the sailor's state and then feature on the web page regattas proximal to that sailor.

    Sample Syntax to Use a Cookie for Database Look-up

    Cookies are used to obtain specific information from a database by including them in a SQL statement WHERE clause. This is usually performed over two blocks of code, the first is to get the cookie into a variable, and the second is to use the contents of that variable in the WHERE clause of a SQL statement that gets data from the specific record of the visitor:

    <%
      Dim varCookieHolder
      varCookieHolder=Request.Cookies("MyCookie")
      Dim oRecordSet
      Set oRecordSet = Server.CreateObject("ADODB.Recordset")
      sqltext = "SELECT * FROM MyTable WHERE RecordID=" &  varCookieHolder
      oRecordSet.Open sqltext, "DSN=MyDataSource"
      Response.Write "Data from Table is " & oRecordSet
     ("MyField")
    %>

    The most common problem here is failure of the SQL statement when the cookie's data is different from expectations. For example, since MyCookie data could hold an ID for a record that has been deleted, you should check that the resulting recordset has a record prior to trying to use it. An additional problem could be when there is no cookie in the request. This condition must be tested and resolved prior to running a SQL statement with an empty WHERE clause.

    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

    - 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...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





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