A DataSet has a MergeFailed event that occurs when merging two DataSets fails. MergeFailedEventHandler handles the MergeFailed event, which receives an argument of type MergeFailedEventArgs containing data related to this event. The MergeFailedEventHandler is defined as follows:
Public Delegate Sub MergeFailedEventHandler(_ ByVal sender As Object, _ ByVal e As MergeFailedEventArgs _ )
In this example, sender is the source of the event, and e is the MergeFailedEventArgs object that contains the event data.
MergeFailedEventArgs has Conflict and Table properties. The Conflict property returns a description of the merge conflict, and the Table property returns the name of the data table. Listing 5-8 shows an example of the MergeFailed event handler.
Listing 5-8: Writing Code to Call MergeFailed Event Handler
ByVal e As System.EventArgs) Handles MergetFailedEventBtn.Click Dim ConnectionString As String = "Integrated Security=SSPI;" & _ "Initial Catalog=Northwind;Data Source=MCB;" Dim conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConnectionString Dim sql As String = "SELECT * FROM Employees" Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn) Dim ds1 As DataSet = New DataSet() da.Fill(ds1) sql = "SELECT * FROM Customers" da = New SqlDataAdapter(sql, conn) Dim ds2 As DataSet = New DataSet() da.Fill(ds2) ' Add MergeFailed Event handler AddHandler ds1.MergeFailed, New MergeFailedEventHandler _ (AddressOf DataSetMergeFailed) ds1.Merge(ds2) 'Remove the MergeFailed handler RemoveHandler ds1.MergeFailed, AddressOf DataSetMergeFailed conn.Close() conn.Dispose() End Sub ' MergeFailed event handler Private Shared Sub DataSetMergeFailed(ByVal sender As Object, _ ByVal args As MergeFailedEventArgs) MessageBox.Show("Merge failed for table " & args.Table.TableName & _ "Conflict = " & args.Conflict) End Sub
As you can see from Listing 5-8, you write an event handler that will be called when a Merge method on a DataSet fails.