Walkthrough: Creating a Windows Form to Search Data

A common application scenario is to display selected data on a form. For example, you might want to display the orders for a specific customer or the details of a specific order. In this scenario, a user enters information into a form, and then a query is executed with the user's input as a parameter; that is, the data is selected based on a parameterized query. The query returns only the data that satisfies the criteria entered by the user. This walkthrough shows how to create a query that returns customers in a specific city, and modify the user interface so that users can enter a city's name and press a button to execute the query.

Using parameterized queries helps make your application efficient by letting the database do the work it is best at — quickly filtering records. In contrast, if you request an entire database table, transfer it over the network, and then use application logic to find the records you want, your application can become slow and non-efficient.

You can add parameterized queries to any TableAdapter (and controls to accept parameter values and execute the query) using the Search Criteria Builder Dialog Box. Open the dialog box by selecting the Add Query command on the Data menu (or on any TableAdapter smart tag).

Tasks illustrated in this walkthrough include:

In order to complete this walkthrough, you need:

The first step is to create a Windows Application. Assigning a name to the project is optional at this step but we will give it a name because we are planning on saving it later.

To create the new Windows Application project

  1. From the File menu, create a new project.

  2. Name the project WindowsSearchForm.

  3. Select Windows Application and click OK. For more information, see Developing Client Applications.

    The WindowsSearchForm project is created and added to Solution Explorer.

This step creates a data source from a database using the Data Source Configuration Wizard. You must have access to the Northwind sample database to create the connection. For information on setting up the Northwind sample database, see How to: Install Sample Databases.

To create the data source

  1. On the Data menu, click Show Data Sources.

  2. In the Data Sources window, select Add New Data Source to start the Data Source Configuration Wizard.

  3. Select Database on the Choose a Data Source Type page, and then click Next.

  4. On the Choose your Data Connection page do one of the following:

    • If a data connection to the Northwind sample database is available in the drop-down list, select it.

      -or-

    • Select New Connection to launch the Add/Modify Connection dialog box.

  5. If your database requires a password, select the option to include sensitive data, and then click Next.

  6. Click Next on the Save connection string to the Application Configuration file page.

  7. Expand the Tables node on the Choose your Database Objects page.

  8. Select the Customers table, and then click Finish.

    The NorthwindDataSet is added to your project and the Customers table appears in the Data Sources window.

You can create the data-bound controls by dragging items from the Data Sources window onto your form.

To create data-bound controls on the form

  1. Expand the Customers node in the Data Sources window.

  2. Drag the Customers node from the Data Sources window to your form.

    A DataGridView and a tool strip (BindingNavigator) for navigating records appear on the form. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.

You can add a WHERE clause to the original query using the Search Criteria Builder Dialog Box.

To create a parameterized query and controls to enter the parameters

  1. Select the DataGridView control, and then choose Add Query on the Data menu.

  2. Type FillByCity in the New query name area on the Search Criteria Builder Dialog Box.

  3. Add WHERE City = @City to the query in the Query Text area.

    The query should be similar to the following:

    SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax

    FROM Customers

    WHERE City = @City

    Note Note

    Access and OleDb data sources use the question mark '?' to denote parameters, so the WHERE clause would look like this: WHERE City = ?.

  4. Click OK to close the Search Criteria Builder dialog box.

    A FillByCityToolStrip is added to the form.

Running the application opens your form ready to take the parameter as input.

To test the application

  1. Press F5 to run the application.

  2. Type London into the City text box and then click FillByCity.

    The data grid is populated with customers that meet the parameterization criteria. In this example, the data grid only displays customers that have a value of London in their City column.

Depending on your application requirements, there are several steps you may want to perform after creating a parameterized form. Some enhancements you could make to this walkthrough include:

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft