Data Lookup
Dependent Lookups

The picture below shows the schema for a sample web application. The Cars database stores several makes of cars, which each have several models. Each model has various styles.

Cars database diagram. Make, model, and style foreign key fields are present to the respective tables.

When a user creates a car, they must select a make, a model, and a style in that order.  The default web application generated from this database will allow the user to select any make, model, or style at any time. The preferred behavior would be to cascade each lookup selection as a filter to the next lookup. When a user changes a master lookup, all child lookups should be cleared.

The script to create the database is attached at the end of this article.

Implementing a Cascading Lookup

Start the Project Designer. In the Project Explorer, switch to the Controllers tab. Double-click on Cars / Fields / MakeID* (Guid) –> Make field node.

Selecting the MakeID field of Cars controller in the Project Designer.

Change the following properties. The Copy property will clear the dependent lookups when the value of this field changes.

Property New Value
Copy

ModelID = null
ModelModelName = null
StyleID = null
StyleStyleName = null

Press OK to save. Double-click on Cars / Fields / ModelID*(Guid) –> Model field node.

ModelID field node of Cars controller.

Change the following properties. The Context Fields property will pass a filter to the lookup.

Property New Value
Context Fields MakeID=MakeID
Copy

StyleID = null
StyleStyleName = null

Press OK to save. Double-click on Cars / Fields / StyleID* (Guid) –> Style field node.

StyleID field of Cars data controller.

Change the following:

Property New Value
Context Fields ModelID=ModelID

Save the field. On the toolbar, press Browse.

Navigate to the Cars page and create a new record. Activate the lookup for Make Name. Select one of the choices.

Selecting a lookup value for make.

Activate the lookup for Model Name. The list of models has been filtered by the previous lookup selection.

Selecting a lookup value for model.

The same filtering will occur on the next lookup.

Selecting a lookup value for style.

Change the make or model. The lookups under the changed value will be cleared.

Changing the make will clear the model and style.

Script for Cars Database

Paste the script below in SQL Server Management Studio and press Execute to create the database.

CREATE DATABASE [CarsExample]
GO
USE [CarsExample]
GO

CREATE TABLE [dbo].[Cars](
    [CarID] [uniqueidentifier] NOT NULL PRIMARY KEY DEFAULT (newid()),
    [MakeID] [uniqueidentifier] NOT NULL,
    [ModelID] [uniqueidentifier] NOT NULL,
    [StyleID] [uniqueidentifier] NOT NULL)
GO
CREATE TABLE [dbo].[Make](
    [MakeID] [uniqueidentifier] NOT NULL PRIMARY KEY DEFAULT (newid()),
    [MakeName] [nvarchar](50) NOT NULL)
GO
CREATE TABLE [dbo].[Model](
    [ModelID] [uniqueidentifier] NOT NULL PRIMARY KEY DEFAULT (newid()),
    [ModelName] [nvarchar](50) NOT NULL,
    [MakeID] [uniqueidentifier] NOT NULL)
GO
CREATE TABLE [dbo].[Style](
    [StyleID] [uniqueidentifier] NOT NULL PRIMARY KEY DEFAULT (newid()),
    [StyleName] [nvarchar](50) NOT NULL,
    [ModelID] [uniqueidentifier] NOT NULL)
GO

ALTER TABLE [dbo].[Cars]  WITH CHECK ADD  CONSTRAINT [FK_Cars_Make] FOREIGN KEY([MakeID])
REFERENCES [dbo].[Make] ([MakeID])
GO
ALTER TABLE [dbo].[Cars]  WITH CHECK ADD  CONSTRAINT [FK_Cars_Model] FOREIGN KEY([ModelID])
REFERENCES [dbo].[Model] ([ModelID])
GO
ALTER TABLE [dbo].[Cars]  WITH CHECK ADD  CONSTRAINT [FK_Cars_Style] FOREIGN KEY([StyleID])
REFERENCES [dbo].[Style] ([StyleID])
GO
ALTER TABLE [dbo].[Model]  WITH CHECK ADD  CONSTRAINT [FK_Model_Make] FOREIGN KEY([MakeID])
REFERENCES [dbo].[Make] ([MakeID])
GO
ALTER TABLE [dbo].[Style]  WITH CHECK ADD  CONSTRAINT [FK_Style_Model] FOREIGN KEY([ModelID])
REFERENCES [dbo].[Model] ([ModelID])
GO