Udostępnij za pośrednictwem


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

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.

Prerequisites

In order to complete this walkthrough, you will need:

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

  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 Creating Windows-Based Applications.

    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

  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.

      -or-

    • Select New Connection to open the Add/Modify Connection dialog box. For more information, see Add/Modify Connection Dialog Box (General).

  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.

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

  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.

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

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

  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:

    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

  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.

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:

See Also

Concepts

What's New in Data Application Development

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

Validating Data

Saving Data

Other Resources

Data Walkthroughs

Connecting to Data in Visual Studio