Share via


Add code to TableAdapters in n-tier applications

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can extend the functionality of a TableAdapter by creating a partial class file for the TableAdapter and adding code to it (instead of adding code to the DatasetName.DataSet.Designer file). Partial classes enable code for a specific class to be divided among multiple physical files. For more information, see Partial or partial (Type).

The code that defines a TableAdapter is generated every time changes are made to the TableAdapter. This code is also generated when changes are made during the running of any wizard that modifies the configuration of the TableAdapter. To prevent your code from being deleted during the regeneration of a TableAdapter, add code to the partial class file of the TableAdapter.

By default, after you separate the dataset and TableAdapter code, the result is a discrete class file in each project. The original project has a file named DatasetName.Designer.vb (or DatasetName.Designer.cs) that contains the TableAdapter code. The project that's designated in the Dataset Project property has a file named DatasetName.DataSet.Designer.vb (or DatasetName.DataSet.Designer.cs) that contains the dataset code.

Note

When you separate datasets and TableAdapters (by setting the DataSet Project property), existing partial dataset classes in the project will not be moved automatically. Existing dataset partial classes must be moved manually to the dataset project.

Note

The DataSet Designer provides functionality for generating ColumnChanging and RowChanging event handlers when validation is needed. For more information, see Add validation to an n-tier dataset.

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 Personalizing the IDE.

To add user code to a TableAdapter in an n-tier application

  1. Locate the project that contains the .xsd file (the dataset).

  2. Double click the .xsd file to open the dataset.

  3. Right-click the TableAdapter that you want to add code to, and then selectView Code.

    A partial class is created and opens in the Code Editor.

  4. Add code inside the partial class declaration.

  5. The following example shows where to add code to the CustomersTableAdapter in the NorthwindDataSet:

    Partial Public Class CustomersTableAdapter
        ' Add code here to add functionality
        ' to the CustomersTableAdapter.
    End Class
    
    public partial class CustomersTableAdapter
    {
        // Add code here to add functionality
        // to the CustomersTableAdapter.
    }
    

See Also

N-Tier Data Applications Overview Add code to datasets in n-tier applications TableAdapters TableAdapterManager Overview Hierarchical Update Overview