Wife/Girlfriend/Sister?
Pepper Spray &
Stun Gun Specials!
KEEP THEM SAFE.
 CoverYourASP --> Displaying foreign keys as list boxes --> Part 3

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
36 active users
2090 visitors today
4757 pages today
(only part of today)
Tools I use

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

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

Creating the list boxes

Having got an array with the foreign key information in them, let's make up the HTML for the list boxes that contain the data.

Note: The code that follows makes an assumption about how your tables are designed. The foreign tables MUST have the values that will appear in the list box as the second field. (It is implied, though not necessary, that the first field is the primary key)

for ( i=0; i<nForeign; i++ )
{
   DBGetRecords ( 'SELECT * FROM ' + sForeignTables [ i ] );

   try
   {
      var sList = '<select name="' + sRefColumns [ i ] + '">';

      while ( !oRecordSet.EOF )
      {
         // I assume that the second field is the one to show in dropdown list
         sList += '<option value="' + oRecordSet ( sForeignColumns [ i ] ) + '">'
         sList += oRecordSet ( 1 ) + '</option>';

         oRecordSet.MoveNext  ( );
      }

      sList += '</select>';

      Application ( sRefTables [ i ] + ':' + sRefColumns [ i ] ) = sList;
   }
   catch ( e )
   {
      DebugOut ( '<p>Failed to create dropdown list for ' + sRefTables[i] + ':' + sRefColumns[i]);
   }
}

Here I loop through the foreign keys, first getting all the values from the foreign table with a simple SELECT query. I then wrap the code in a try..catch statement to catch any errors that occur when accessing the recordset - for example if the table has no second column.

The data in the recordset is then used to fill the sList variable with the relevant <select><option></option>...</select> HTML for the list boxes.

The sList variable is then stored in an Application variable whose name is formed from the primary table name and the relevant field name.

As an example, here is the actual data stored in my live Application ( 'Cars:MakeID' ) variable:

Which, when used on a page becomes this: undefined

The catch statement simply displays an error message when the page is being debugged - read more about this in my article.

      DBReleaseConnection ( );

      Application ( 'GatheredForeignKeys' ) = true;
   }
}

Lastly, the connection to the database is released and the GatheredForeignKeys variable set to stop this happening again until the server is restarted (when all Application variables will be lost).

Part 4: Using the list boxes...

Featured sponsor
My favorite resources

Tiki Statues - Tiki Masks - Tiki Totems



Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


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!


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...