Share via


Handling DataSet Events (ADO.NET)

The DataSet object provides three events: Disposed, Initialized, and MergeFailed.

The MergeFailed Event

The most commonly used event of the DataSet object is MergeFailed, which is raised when the schema of the DataSet objects being merged are in conflict. This occurs when a target and source DataRow have the same primary key value, and the EnforceConstraints property is set to true. For example, if the primary key columns of a table being merged are the same between the tables in the two DataSet objects, an exception is thrown and the MergeFailed event is raised. The MergeFailedEventArgs object passed to the MergeFailed event have a Conflict property that identifies the conflict in schema between the two DataSet objects, and a Table property that identifies the name of the table in conflict.

The following code fragment demonstrates how to add an event handler for the MergeFailed event.

AddHandler workDS.MergeFailed, New MergeFailedEventHandler( _
  AddressOf DataSetMergeFailed)

Private Shared Sub DataSetMergeFailed(  _
  sender As Object,args As MergeFailedEventArgs)
  Console.WriteLine("Merge failed for table " & args.Table.TableName)
  Console.WriteLine("Conflict = " & args.Conflict)
End Sub
workDS.MergeFailed += new MergeFailedEventHandler(DataSetMergeFailed);

private static void DataSetMergeFailed(
  object sender, MergeFailedEventArgs args)
{
  Console.WriteLine("Merge failed for table " + args.Table.TableName);
  Console.WriteLine("Conflict = " + args.Conflict);
}

The Initialized Event

The Initialized event occurs after the DataSet constructor initializes a new instance of the DataSet.

The IsInitialized property returns true if the DataSet has completed initialization; otherwise it returns false. The BeginInit method, which begins the initialization of a DataSet, sets IsInitialized to false. The EndInit method, which ends the initialization of the DataSet, sets it to true. These methods are used by the Visual Studio design environment to initialize a DataSet that is being used by another component. You will not commonly use them in your code.

The Disposed Event

DataSet is derived from the MarshalByValueComponent class, which exposes both the Dispose method and the Disposed event. The Disposedevent adds an event handler to listen to the disposed event on the component. You can use the Disposedevent of a DataSet if you want to execute code when the Disposemethod is called. Disposereleases the resources used by the MarshalByValueComponent.

Note

The DataSet and DataTable objects inherit from MarshalByValueComponentand support the ISerializable interface for remoting. These are the only ADO.NET objects that can be remoted. For more information, see Remote Objects.

For information about other events available when working with a DataSet, see Handling DataTable Events (ADO.NET) and Handling DataAdapter Events (ADO.NET).

See Also

Other Resources

DataSets, DataTables, and DataViews (ADO.NET)

Validating Data

Retrieving and Modifying Data in ADO.NET

ADO.NET Managed Providers and DataSet Developer Center