Using a Parameterized Query on a Screen

In this lesson, you will learn how to use a parameterized query on a screen to filter data.

Using a Parameterized Query

In the last lesson, you created a query to filter products based on a CategoryID parameter. At run time, this parameter can be used to apply the filter to another entity that contains a CategoryID field. For example, you can use the parameterized query to display a list of products that match the category of a selected product in a list on the screen.

The parameter value can be supplied as a property in the Screen Designer at design time or by the user at run time. In this example, you will assign the value at design time.

To add a parameterized query to a screen

  1. In Solution Explorer, double-click the CurrentProductList screen node.

  2. In the Screen Designer, click the Add Data Item toolbar button.

    The Add Data Item dialog box appears.

  3. In the Add Data Item dialog box, click Query.

  4. In the Name column, select NorthwindEntitiesData.ProductsByCategory, and then click OK.

    A ProductsByCategoryCollection node is added to the left pane.

  5. Under the Query Parameters node, select CategoryID.

  6. In Properties window, select Parameter Binding and type CurrentProducts.SelectedItem.Category.CategoryID.

  7. In the Screen Designer, select the Rows Layout | Details Column node.

  8. Click Add and select ProductsByCategory from the drop-down list.

    A new DataGrid | ProductsByCategory node appears in the screen content tree.

  9. Press F5 to run the application and view the results.

    When you open the CurrentProductList screen, notice that all products that have the same CategoryID as the currently selected product are displayed in the new grid.

Closer Look

In this procedure, you used the parameterized query that you created in the last lesson to display a list of products. The query was added to an existing screen, the CurrentProductList screen. The Parameter Value property of the query specifies the CategoryID value from the existing ProductCollection on the screen and supplies the parameter at run time. As a result, a list of products that have a matching CategoryID is displayed every time a product is selected in the ProductCollection list.

Next Steps

In the next set of lessons, you will learn how to work with menus and commands.

Next lesson: Creating Menus and Commands

See Also

Tasks

How to: Provide a Value to a Query Parameter

How to: Extend a Query by Using Code

Other Resources

Filtering Data with Queries

Queries: Retrieving Information from a Data Source