ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Displaying foreign keys as list boxes --> 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
148 active users
2905 visitors today
2837 pages today
(only part of today)
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!

Reading the foreign keys

Before I start, I'm assuming that you understand what a Primary Key and a Foreign Key is. If not, read MSDN's description.

My new code consists of 3 functions in my SSI, utils/database.asp. You can download the entire site, including this file, for free.

The first of the three functions is DBGatherForeignKeys, and should be called from any page that will want to display list boxes containing the data defined by the foreign keys. This function queries the database for all the foreign keys, builds the HTML for the list boxes and stores it in Application variables.

function DBGatherForeignKeys ( )
{
   if ( !Application ( 'GatheredForeignKeys' ) )
   {
      DBInitConnection ( );

      oRecordSet = oConnection.OpenSchema ( adSchemaForeignKeys );

Don't worry about calling this function from all your pages - it only does anything the very first time it is called. This is handled by the line that tests if there is a GatheredForeignKeys variable set. If there is then the function does nothing.

The next two lines open the database, and then fill a recordset with a list of all the foreign keys in the database.

var sRefTables = new Array;
var sRefColumns = new Array;
var sForeignTables = new Array;
var sForeignColumns = new Array;
var nForeign = 0;

while ( !oRecordSet.EOF )
{
   sRefTables [ nForeign ] = '' + oRecordSet ( 'FK_TABLE_NAME' );
   sRefColumns [ nForeign ] = '' + oRecordSet ( 'FK_COLUMN_NAME' );
   sForeignTables [ nForeign ] = '' + oRecordSet ( 'PK_TABLE_NAME' );
   sForeignColumns [ nForeign++ ] = '' + oRecordSet ( 'PK_COLUMN_NAME' );

   oRecordSet.MoveNext  ( );
}

Next we create some arrays to hold the information about the foreign keys temporarily, and loop through the recordset filling them up. nForeign is incremented each time, so it ends up holding the number of foreign keys contained in the arrays.

Part 3: Creating the list boxes...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



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!