We recommend using Visual Studio 2017

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:

Note 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.

In order to complete this walkthrough, you will need:

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

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

  2. Name the project UpdateSingleTableWalkthrough.

  3. 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.

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

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

  2. In the Data Sources window, click 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.


    • Select New Connection to open 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.

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

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

  2. 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

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

  1. Double-click the Save button on the BindingNavigator to open the Code Editor to the bindingNavigatorSaveItem_Click event handler.

  2. Replace the code in the event handler to add some error handling. The code should look like the following:

        MessageBox.Show("Update successful");
    catch (System.Exception ex)
        MessageBox.Show("Update failed");

To test the application

  1. Press F5.

  2. Make some changes to the data of one or more records.

  3. Press the Save button.

  4. Check the values in the database to verify that the changes were saved.

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: