Share via


Add code to datasets 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 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 or Partial Classes and Methods.

The code that defines a dataset is generated every time changes are made to the dataset definition. 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 filenamed 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 that's named DatasetName.DataSet.Designer.vb (or DatasetName.DataSet.Designer.cs).This file contains the dataset code.

Note

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

Note

When validation code needs to be added, the DataSet Designer provides functionality for generating ColumnChanging and RowChanging event handlers. For more information, see Add validation to an n-tier dataset.

Add code to datasets in n-tier applications

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

  2. Select the .xsd file to open the dataset.

  3. Right-click the data table to which you want to add code (the table name in the title bar), and then select 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