How to: Add Code to TableAdapters in N-Tier Applications

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 (Visual Basic) or partial (Type) (C# Reference).)

The code that defines a TableAdapter is generated every time changes are made to the TableAdapter (in the Dataset Designer). 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 that is named DatasetName.Designer.vb (or DatasetName.Designer.cs) that contains the TableAdapter code. The project designated in the Dataset Project property has a file that is named DatasetName.DataSet.Designer.vb (or DatasetName.DataSet.Designer.cs) that contains the dataset code.


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.


The Dataset Designer also provides functionality for generating ColumnChanging and RowChanging event handlers when validation code should be added. For more information, see How to: Add Validation to an N-Tier Dataset.


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.

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

  1. Locate the project that contains the .xsd file (the Dataset Designer).

  2. Double click the .xsd file to open the Dataset Designer.

  3. Right-click the TableAdapter that you want to add code to and click View 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.

Community Additions