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.
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 Customizing Development Settings in Visual Studio.
Prerequisites
In order to complete this walkthrough, you will need:
- Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
Creating the Windows Application
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 Developing Client Applications with the .NET Framework.
The UpdateSingleTableWalkthrough project is created and added to Solution Explorer.
Creating the Data Source
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.
-or-
Select New Connection to open the Add/Modify Connection dialog box.
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.
Setting the Controls to be Created
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.
Creating the Data-bound Form
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.
Modifying the Code to Update the Database
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:
Try Me.Validate() Me.CustomersBindingSource.EndEdit() Me.CustomersTableAdapter.Update(Me.NorthwindDataSet.Customers) MsgBox("Update successful") Catch ex As Exception MsgBox("Update failed") End Try
try { this.Validate(); this.customersBindingSource.EndEdit(); this.customersTableAdapter.Update(this.northwindDataSet.Customers); MessageBox.Show("Update successful"); } catch (System.Exception ex) { MessageBox.Show("Update failed"); }
Testing the Application
To test the application
Press F5.
Make some changes to the data of one or more records.
Press the Save button.
Check the values in the database to verify that the changes were saved.
Next Steps
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.
See Also
Concepts
Binding Windows Forms Controls to Data in Visual Studio
Preparing Your Application to Receive Data
Fetching Data into Your Application
Binding Controls to Data in Visual Studio
Editing Data in Your Application