Internet.comReal World Active Server Pages Solutions and Resources 
ASPWatch.com
Sponsored by Compuware Corporation
Essential survival skills for Internet success!
ScriptingDatabaseComponentsXMLIntegrationSolutions

Newsletter
Click here!
Search


Pete Nelson's Home Page
Log off

internet.com
Internet News
Internet Investing
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers


Image & Video Software
Know your Web users
Find a Consultant
Database Web Tools
Web System Integrator
Internet Intelligence
IT Solutions
Add Chat To My Site
Add Online Sales Reps
Volume Licensing

Caching Select-Box Data
by Pete Nelson
Skill level: Beginner
First posted: Thursday, May 11, 2000
Overview

A common technique used by developers is to store select-box data in a table in a database, then generate the select-box from the database each time it is needed. Items such as a list of states, countries, shipping methods, etc are perfect examples. This sort of data is needed frequently but does not change very often. For this sort of "static" data, you can cache the data once and can bypass querying the server each time it is needed.


Generating Select-Boxes from Lookup Tables

I call any tables that are used in this manner Lookup Tables. For my own naming convention, I prefix the table with "LK_" so I know it's a lookup table. My LK_ShippingMethods table would look something like this:

Figure


Normal generation the select-box would generate would loop through a recordset and look like this:

<%
Dim recShipping
Set recShipping = Server.CreateObject("ADODB.Recordset")
recShipping.Source = "Select * from LK_ShippingMethods order by SortOrder"
recShipping.ActiveConnection = conDB
recShipping.Open
%>
<select name="ShippingMethod">
<%
do while not recShipping.EOF
   %><option value="<% =recShipping("ID") %>"><% =Trim(recShipping("Description")) %></option>
   <%
   recShipping.MoveNext
loop
recShipping.Close
Set recShipping = Nothing
%>
</select>


and would generate HTML that looked like this:

<select name="ShippingMethod">
<option value="1">USPS Ground</option>
<option value="2">UPS 2-Day</option>
<option value="3">Fedex Overnight</option>
</select>

This makes generating and maintaining select-boxes a snap. But the drawback is that we have to query the database each we want to create a select-box from a table.

Caching the Data

The method I use to cache data for select-boxes is to store the data in an array, then store the array in an Application variable. You can then use a For...Next loop using the LBound and UBound of the array to loop through the data, rather than creating and looping through a recordset.

I populate my arrays in the Application_OnStart sub in Global.asa. First we get the same recordset as above, then dim an array to match the number of records returned by the query (determined by the RecordCount property). As we loop through the recordset, we use the AbsolutePosition property to determine which position in the array to assign the value. AbsolutePosition is a 1-based value, so we subtract 1 from it to match the 0-based array. We use a cursor-type of 1 (keyset) to get an accurate RecordCount property.


<%
Sub Application_OnStart()

'*** Add Code here to open the conDB connection object

Dim recShipping, arrShipping
Set recShipping = Server.CreateObject("ADODB.Recordset")
recShipping.Source = "Select * from LK_ShippingMethods order by SortOrder"
recShipping.ActiveConnection = conDB
recShipping.Open , , 1

Redim arrShipping(recShipping.RecordCount, 2)

Do While Not recShipping.EOF

   '*** Put the ID in the first element
   arrShipping(recShipping.AbsolutePosition - 1, 0) = recShipping("ID")

   '*** Put the Description in the second element
   arrShipping(recShipping.AbsolutePosition - 1, 1) = Trim(recShipping("Description"))

   recShipping.MoveNext
Loop

recShipping.Close
Set recShipping = Nothing

Application("arrShipping") = arrShipping

End Sub
%>


Now when we need to generate a select-box for this data, we already have everything in an Application variable. Just do a For...Next loop to generate the select-box.

<%
Dim arrShipping, x
arrShipping = Application("arrShipping")
%>
<select name="ShippingMethod">
<%
For x = LBound(arrShipping, 1) to UBound(arrShipping, 1) - 1
   %><option value="<% =arrShipping(x, 0) %>"><% =arrShipping(x, 1) %></option>
<%
Next
%>
</select>


Say for example that you had a web page that had 4 select-boxes and was accessed 100 times each day. By caching the data for the select-boxes, that's 400 fewer queries and trips to the database.



Pete Nelson
Pete is the webmaster for DVD Tracker, a site that lets people keep an online catalog of their DVD collection. He has been doing ASP development for three years and is proficient in VBScript, SQL and HTML, and dabbles in VB COM, PHP and mySql now and then. He's married to a Linux geek and lives in the San Francisco Bay Area.
What did you think of this article?
Not usefulVery useful
Badly written Well written
Too shortToo long
This document is not listed in any Encyclopedia chapters.
This is what people have been saying about this article:
Caching Select-Box Data - Brad Tenney - Wednesday May 31 8:16:00 AM
   Caching Select-Box Data - Anonymous - Tuesday May 30 12:41:00 AM
GetRows() - Anonymous - Tuesday May 23 11:28:00 AM
   GetRows() - Anonymous - Tuesday May 23 11:28:00 AM
Caching data in Application variable - Anonymous - Wednesday May 31 8:13:00 AM
   Caching data in Application variable - Anonymous - Wednesday May 31 8:13:00 AM
      Caching data in App var - Alternate Method - Anonymous - Wednesday May 24 4:40:00 PM
Apartment mode; error - Anonymous - Friday May 26 2:21:00 PM
   Apartment mode; error - Anonymous - Friday May 19 6:32:00 AM
      Apartment mode; error - Anonymous - Friday May 19 6:32:00 AM
   Apartment mode; error - Anonymous - Friday May 26 2:21:00 PM
Picking an Item in the Select List - Anonymous - Tuesday May 23 1:48:00 PM
Tip: Reverse the Array dimension - Anonymous - Sunday May 28 4:27:00 PM
Thanks for the idea, Pete - Anonymous - Tuesday May 30 12:54:00 AM

Create a new discussion
View Article Statistics

Authors...
Edit this article
View Preview Version
Printable Copy of Article
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Essential survival skills for Internet success!

About your privacy | Want to advertise? | Contact Us

Copyright © internet.com Corporation 2001
http://www.internet.com