Walkthrough: Saving Data to a Database (Single Table)
One of the most common scenarios in application development is to display data on a form in a Windows application, edit the data, and send the updated data back to the database. This walkthrough creates a simple form that displays data from a single table in individual controls. You can edit the data in controls and save your changes back to the database. This example uses the Customers table from the Northwind sample database.
You can save data in your application back to the database by calling the Update method of a TableAdapter. When you drag items from the Data Sources window, code to save data is automatically added for the first table dragged onto a form. Any additional tables added to a form require the manual addition of any code required to save data. For information on saving data from more than one table, see Walkthrough: Saving Data to a Database (Multiple Tables).
Tasks illustrated in this walkthrough include:
Creating a new Windows Application project.
Creating and configuring a data source in your application with the Data Source Configuration Wizard.
Set the controls of the items in the Data Sources Window. For more information, see How to: Set the Control to be Created when Dragging from the Data Sources Window.
Creating data-bound controls by dragging items from the Data Sources window onto your form.
Modifying a couple of records in the dataset.
Adding code to send the updated data in the dataset back to the database.
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
In order to complete this walkthrough, you will need:
Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
The first step is to create a Windows Application. Assigning a name to the project is optional at this step but we are giving it a name because we are planning on saving it later.
To create the new Windows Application project
From the File menu, create a new project.
Name the project UpdateSingleTableWalkthrough.
Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.
The UpdateSingleTableWalkthrough project is created and added to Solution Explorer.
This step creates a data source from the Northwind database using the Data Source Configuration Wizard. You must have access to the Northwind sample database to complete the wizard. For information on setting up the Northwind sample database, see How to: Install Sample Databases.
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.
Select New Connection to open the Add/Modify Connection dialog box. For more information, see 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 table, and then click Finish.
The NorthwindDataSet is added to your project and the Customers table appears in the Data Sources window.
For this walkthrough the data will be in a Details layout where data is displayed in individual controls instead of the default DataGridView layout.
To set the controls for the items in the Data Sources window
Expand the Customers node in the Data Sources window.
Change the control for the Customers table to individual controls by selecting Details from the drop-down list on the Customers node. For more information, see How to: Set the Control to be Created when Dragging from 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
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 (BindingNavigator) for navigating records. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
You can update the database by calling the Update method of the CustomersTableAdapter. By default, an event handler for the BindingNavigator's Save button is added to the form's code to send updates to the database. This procedure modifies the code to include error handling by wrapping the update call in a try-catch block. You can modify the code to suit the needs of your application.
To add update logic to the application
Double-click the Save button on the BindingNavigator to open the Code Editor to the bindingNavigatorSaveItem_Click event handler.
Replace the code in the event handler to add some error handling. The code should look like the following:
Depending on your application requirements, there are several steps you may want to perform after creating a data-bound form in your Windows application. Some enhancements you could make to this walkthrough include:
Adding search functionality to the form. For more information, see How to: Add a Parameterized Query to a Windows Forms Application.
Adding controls that display related data. For more information, see How to: Display Related Data in a Windows Forms Application.
Editing the data source to add or remove database objects. For more information, see How to: Edit a Dataset.