Data Access in Client and Middle-Tier Programming
Walkthrough: Creating a Simple Data Application
One of the most common scenarios in application development is to display data from a database on a Windows Form. You can display data on forms in Windows applications by dragging items from the
Data Sources Window onto your form. This walkthrough demonstrates how to create an application that displays data from two related tables in a database.
Note The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose
Import and Export Settings on the
Tools menu. For more information, see
Visual Studio Settings.
Tasks illustrated in this walkthrough include:
- Creating a Windows application.
- Creating and configuring a dataset in your application based on the Customers and Orders tables in the Northwind database using the Data Source Configuration Wizard.
- Adding controls to display data from the Customers table.
- Adding controls to display the Orders based on the selected Customer.
- Testing the application, selecting different customers and verifying that the correct orders are shown for the selected customer.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl101c9b3b9,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl10img,";
Prerequisites
In order to complete this walkthrough, you will need:
- Access to the Northwind sample database. To setup sample databases, see <A onclick="javascript:Track('ctl00_LibFrame_ctl10|ctl00_LibFrame_ctl11',this);" href="http://msdn2.microsoft.com/en-us/library/8b6y4c7s(VS.80).aspx">How to: Install Sample Databases.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl12258c4a3,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl12img,";
Creating the Project
The first step is to create a
Windows Application project.
To create the project
- From the File menu, create a new project.
- Select Windows Application and name it DataWalkthrough. For more information, see <A onclick="javascript:Track('ctl00_LibFrame_ctl12|ctl00_LibFrame_ctl13',this);" href="http://msdn2.microsoft.com/en-us/library/54xbah2z(VS.80).aspx">Creating Windows-based Applications.
- Click OK.
The DataWalkthrough project is created and added to Solution Explorer.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl14b4d596f,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl14img,";
Creating the Data Source
This step creates a dataset based on the Customers and Orders tables in the Northwind sample database.
To create the data source
- On the Data menu, click Show Data Sources.
- In the Data Sources window, click Add New Data Source to start the Data Source Configuration Wizard.
- Select Database on the Choose a Data Source Type page, and then click Next.
- 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. For more information, see <A onclick="javascript:Track('ctl00_LibFrame_ctl14|ctl00_LibFrame_ctl15',this);" href="http://msdn2.microsoft.com/en-us/library/c3t1z354(VS.80).aspx">Add/Modify Connection Dialog Box (General).
- If your database requires a password, select the option to include sensitive data, and then click Next.
- Click Next on the Save connection string to the Application Configuration file page.
- Expand the Tables node on the Choose your Database Objects page.
- Select the Customers and Orders tables, and then click Finish.
The NorthwindDataSet is added to your project and the Customers and Orders tables appear in the Data Sources window.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl1687e1035,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl16img,";
Creating Controls to Display Data from the Customers Table
To create controls to display the customer data (parent records)
- In the Data Sources window, select the Customers table, and then click the drop-down arrow.
- Select Details from the Customer table's control list.
- Drag the main Customers node from the Data Sources window onto Form1.
Data-bound controls with descriptive labels appear on the form, along with a tool strip (<A onclick="javascript:Track('ctl00_LibFrame_ctl16|ctl00_LibFrame_ctl17',this);" href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingnavigator(VS.80).aspx">BindingNavigator) for navigating records. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl219a44aef,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl21img,";
Creating Controls to Display Data from the Orders Table
To create controls to display the orders for each customer (child records)
- In the Data Sources window, expand the Customers node and select the last column in the Customers table, which is an expandable Orders node beneath the Fax column, and drag it onto the bottom of Form1. (This node in the Customers table hierarchy represents the related orders for a customer, as opposed to the main Orders node, which represents all records in the Orders table and not the orders for an individual customer.)
A <A onclick="javascript:Track('ctl00_LibFrame_ctl21|ctl00_LibFrame_ctl22',this);" href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview(VS.80).aspx">DataGridView is added to the form, and a new BindingSource component (OrdersBindingSource) and TableAdapter (OrdersTableAdapter) are added to the component tray.
Note Open the Properties window and select the OrdersBindingSource component. Inspect the DataSource and DataMember properties to see how data binding is configured to display related records. The DataSource is set to the CustomersBindingSource (the parent table's BindingSource), as opposed to the Orders table. The DataMember property is set to FK_Orders_Customers, which is the name of the DataRelation object that relates the tables to each other.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl267a49e2c,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl26img,";
Testing the Application
To test the application
- Press F5.
- Select different customers to verify that the correct orders are displayed in the grid on the form.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl2767c9073,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl27img,";<A class=CollapseRegionLink onclick="ShowHideCollapsibleArea('ctl00_LibFrame_ctl2767c9073','ctl00_LibFrame_ctl27img');return false;" href="http://msdn2.microsoft.com/en-us/library/ms171884(VS.80).aspx#">
Next Steps
Depending on your application requirements, there are several steps you may want to perform after creating a master-detail form. Some enhancements you could make to this walkthrough include:
- Editing the query that loads data into the application with the TableAdapter wizards. For more information, see <A onclick="javascript:Track('ctl00_LibFrame_ctl27|ctl00_LibFrame_ctl28',this);" href="http://msdn2.microsoft.com/en-us/library/ms171903(VS.80).aspx">How to: Edit TableAdapter Queries, and How to: Edit TableAdapters.
- Adding validation to verify the correct data is being entered before sending changes to the database. For more information, see Validating Data.
var ExpCollDivStr = ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl31719461f,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl31img,";
See Also
Concepts
<A onclick="javascript:Track('ctl00_LibFrame_ctl31|ctl00_LibFrame_ctl32',this);" href="http://msdn2.microsoft.com/en-us/library/87sfaezs(VS.80).aspx">
What's New in Data
Displaying Data Overview
Other Resources
Data Walkthroughs
Getting Started with Data Access
Connecting to Data in Visual Studio
Preparing Your Application to Receive Data
Fetching Data into Your Application
Displaying Data on Forms in Windows Applications
Editing Data in Your Application
Validating Data
Saving Data