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

You can extend the functionality of a dataset by creating a partial class file for the dataset 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 Classes and Methods (C# Programming Guide).)

The code that defines a dataset is generated every time changes are made to the dataset definition (in the Creating and Editing Typed Datasets). This code is also generated when you make changes during the running of any wizard that modifies the configuration of a dataset. To prevent your code from being deleted during the regeneration of a dataset, add code to the dataset's partial class file.

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 Creating and Editing Typed Datasets 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.

To add code to datasets in n-tier applications

  1. Locate the project that contains the .xsd file (the Creating and Editing Typed Datasets).

  2. Double click the .xsd file to open the Creating and Editing Typed Datasets.

  3. Right-click the data table to which you want to add code (the table name in the title bar) and click View Code.

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

  4. Add code inside the partial class declaration.

    The following example shows where to add code to the CustomersDataTable in the NorthwindDataSet:

    Partial Public Class CustomersDataTable
        ' Add code here to add functionality 
        ' to the CustomersDataTable.
    End Class
    partial class CustomersDataTable
        // Add code here to add functionality
        // to the CustomersDataTable.

See Also


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


N-Tier Data Applications Overview

TableAdapterManager Overview

Hierarchical Update Overview

Creating Data Applications

Working with Datasets in Visual Studio

Other Resources