Share via


How to: Implement Hierarchical Update in Existing Visual Studio Projects

Starting in Visual Studio 2008, typed datasets are enhanced by the TableAdapterManager component. TableAdapterManager reduces the code that is required for saving data in multiple related tables from several routines with many lines of code to a single method call: TableAdapterManager.UpdateAll(TypedDataset). By default, hierarchical update is enabled for all new datasets that are added to or created in a project. Existing datasets (datasets created in versions of Visual Studio before Visual Studio 2008) do not contain a TableAdapterManager component. Therefore, by default, datasets created in earlier versions have their Hierarchical Update property set to False. Generate TableAdapterManager components by setting the Hierarchical Update property to True and saving the dataset. For more information, see TableAdapterManager Overview.

Enabling Hierarchical Update and Generating the TableAdapterManager Component

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.

To enable hierarchical update in earlier versions of typed datasets

  1. Open the project that contains a typed dataset or datasets in Visual Studio.

  2. Complete the Visual Studio Conversion Wizard.

  3. Open the dataset in the Dataset Designer by double-clicking the .xsd file in Solution Explorer.

  4. Click an empty area on the Dataset Designer.

  5. Locate the Hierarchical Update property in the Properties Window.

  6. Set the value of the Hierarchical Update property to True.

  7. On the Build menu, click Build Solution.

Configuring the TableAdapterManager and Saving Data

Setting the Hierarchical Update property to True and saving the dataset generates the TableAdapterManager component that enables hierarchical update. After you generate the TableAdapterManager component, update your existing code to instantiate a new TableAdapterManager and call the TableAdapterManager.UpdateAll() method instead of calling the individual TableAdapter.Update() methods. If the existing TableAdapter code is in a class library (or any file that does not have a user interface) you must programmatically instantiate a new TableAdapterManager. If the existing TableAdapter code is on a form, you can instantiate a new TableAdapterManager by dragging a TableAdapterManager component from the Toolbox onto the form.

TableAdapterManagers are strongly typed, which means they are generated with properties that reference the individual TableAdapters that they manage. After you instantiate a TableAdapterManager, set the individual TableAdapter properties to instances of each TableAdapter, as shown in the following procedures.

To add a TableAdapterManager to an existing project without a user interface

  1. Locate the code that calls the TableAdapter.Update methods (for example, CustomersTableAdapter.Update(NorthwindDataSet.Customers)).

  2. Replace the code that calls the individual TableAdapter.Update methods with the following:

    Dim adapterManager as new TableAdapterManager
    adapterManager.TableName1TableAdapter = new _
    TableName1TableAdapter
    adapterManager.TableName2TableAdapter = new _
    TableName2TableAdapter
    
    adapterManager.UpdateAll(Me.DataSetName)
    
    TableAdapterManager adapterManager = 
    new TableAdapterManager();
    
    adapterManager.TableName1TableAdapter = 
    new TableName1TableAdapter();
    adapterManager.TableName2TableAdapter = 
    new TableName2TableAdapter();
    
    adapterManager.UpdateAll(this.DataSetName);
    

To add a TableAdapterManager to an existing project that has a user interface

  1. Open the form in Design view.

  2. Drag a TableAdapterManager component from the Toolbox onto the form.

  3. Locate the code that calls the TableAdapter.Update methods (for example, CustomersTableAdapter.Update(NorthwindDataSet.Customers)).

  4. Replace the code that calls the individual TableAdapter.Update methods with the following:

    Me.TableAdapterManager.TableName1TableAdapter = _
    new DatasetNameTableAdapters.TableName1TableAdapter
    Me.TableAdapterManager.TableName2TableAdapter = _
    new DatasetNameTableAdapters.TableName2TableAdapter
    
    Me.TableAdapterManager.UpdateAll(Me.DataSetName)
    
    TableAdapterManager.TableName1TableAdapter = new 
    TableName1TableAdapter();
    TableAdapterManager.TableName2TableAdapter = new 
    TableName2TableAdapter();
    
    TableAdapterManager.UpdateAll(this.DataSetName);
    

See Also

Concepts

What's New for Data Application Development in Visual Studio 2012

Hierarchical Update Overview

TableAdapterManager Overview