CoverYourASP --> Application variables --> Part 2

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
20 active users
865 visitors today
750 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2016 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

A simple function to store HTML

In the earlier example of populating a list box from a recordset I created a function to execute the query, and store the HTML in Application variables for easy and very efficient re-use:

// ============================================
// put infrequently changing data into application variables
// ============================================
function GetListFromApp ( sSQL, sName)
{
   var sHTML;

   // connect to database
   DBInitConnection ( );

   // get HTML from Application variable
   // and if empty...
   if ( ! ( sHTML = Application ( sName ) ) )
   {
      //...get records from database
      DBGetRecords ( sSQL );

      // make up HTML <select> tag
      sHTML = '<select name="' +sName + '">';

      while ( !oRecordSet.EOF )
      {
         sHTML += '<option>' + oRecordSet ( 0 ) + '</option>';      
         oRecordSet.moveNext ( );
      }

      sHTML += '</select>';

      // set the Application variable
      Application ( sName ) = sHTML;
   }

   // release database connection
   DBReleaseConnection ( );

   return sHTML;
}

To use it to store a list box containing all your professors names, do this:

// form tag posting to itself
Response.Write ( '<form action="' + Request.ServerVariables ( 'SCRIPT_NAME' ) + '" method="post">' );

// a list box containing professors - only
// does database query once
Response.Write ( 'Department code: ' + GetListFromApp ( 'SELECT Name FROM Professors', 'prof' ) );

Part 3: Counting visits and page views...

Featured sponsor
My favorite resources


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!