ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 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
146 active users
3114 visitors today
4674 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

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

ASP.NET Blog
ASPRSS
ShawThing

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

Selling products online - read how James does it now



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!


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!