Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataSet.MergeFailed Event

Occurs when a target and source DataRow have the same primary key value, and EnforceConstraints is set to true.

[Visual Basic]
Public Event MergeFailed As MergeFailedEventHandler
[C#]
public event MergeFailedEventHandler MergeFailed;
[C++]
public: __event MergeFailedEventHandler* MergeFailed;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type MergeFailedEventArgs containing data related to this event. The following MergeFailedEventArgs properties provide information specific to this event.

Property Description
Conflict Returns a description of the merge conflict.
Table Returns the DataTable object.

Remarks

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following example demonstrates the use of the MergeFailed event.

[Visual Basic] 

Private Shared Sub DemonstrateMergeFailedEvent()
    ' Create a DataSet with one table containing two columns.
    Dim ds AS DataSet = New DataSet("myDataSet")
    Dim t As DataTable = New DataTable("Items")
    
    ' Add table to the DataSet.
    ds.Tables.Add( t )

    ' Add two columns to the DataTable.
    t.Columns.Add( "id", Type.GetType("System.Int32") )
    t.Columns.Add( "item", Type.GetType("System.Int32") )

    ' Set the primary key to the first column.
    t.PrimaryKey = new DataColumn() { t.Columns("id") }

    ' Add MergeFailed event handler for the table.
    AddHandler ds.MergeFailed, New MergeFailedEventHandler( AddressOf Merge_Failed )

    ' Create a second DataTable identical to the first, 
    Dim t2 As DataTable = t.Clone()

    ' Set the primary key of the new table to the second column.
    ' This will cause the MergeFailed event to be raised when the
    ' table is merged into the DataSet.
    t2.PrimaryKey = New DataColumn() { t2.Columns("item") }
    
    ' Merge table into the DataSet.
    Console.WriteLine( "Merging..." )
    ds.Merge( t2, false, MissingSchemaAction.Add )
End Sub

Private Shared Sub Merge_Failed( sender As object, e As MergeFailedEventArgs )
    Console.WriteLine( "Merge_Failed Event: '{0}'", e.Conflict )
End Sub


[C#] 

private static void DemonstrateMergeFailedEvent()
{
    // Create a DataSet with one table containing two columns.
    DataSet ds = new DataSet("myDataSet");
    DataTable t = new DataTable("Items");
    
    // Add table to the DataSet.
    ds.Tables.Add( t );

    // Add two columns to the DataTable.
    t.Columns.Add( "id", typeof(int) );
    t.Columns.Add( "item", typeof(int) );

    // Set the primary key to the first column.
    t.PrimaryKey = new DataColumn[] { t.Columns["id"] };

    // Add MergeFailed event handler for the table.
    ds.MergeFailed += new MergeFailedEventHandler( Merge_Failed );

    // Create a second DataTable identical to the first, 
    DataTable t2 = t.Clone();

    // Set the primary key of the new table to the second column.
    // This will cause the MergeFailed event to be raised when the
    // table is merged into the DataSet.
    t2.PrimaryKey = new DataColumn[] { t2.Columns["item"] };
    
    // Merge the table into the DataSet.
    Console.WriteLine( "Merging..." );
    ds.Merge( t2, false, MissingSchemaAction.Add );
}

private static void Merge_Failed( object sender, MergeFailedEventArgs e )
{
    Console.WriteLine( "Merge_Failed Event: '{0}'", e.Conflict );
}


[C++] 

public:
    static void DemonstrateMergeFailedEvent() {
        // Create a DataSet with one table containing two columns.
        DataSet* ds = new DataSet(S"myDataSet");
        DataTable* t = new DataTable(S"Items");

        // Add table to the DataSet.
        ds->Tables->Add(t);

        // Add two columns to the DataTable.
        t->Columns->Add(S"id", __typeof(int));
        t->Columns->Add(S"item", __typeof(int));

        // Set the primary key to the first column.
        DataColumn* FirstColumn[] = { t->Columns->Item[S"id"] };
        t->PrimaryKey = FirstColumn;

        // Add MergeFailed event handler for the table.
        ds->MergeFailed += new MergeFailedEventHandler(0, Merge_Failed);

        // Create a second DataTable identical to the first, 
        DataTable* t2 = t->Clone();

        // Set the primary key of the new table to the second column.
        // This will cause the MergeFailed event to be raised when the
        // table is merged into the DataSet.
        DataColumn* SecondColumn[] = { t2->Columns->Item[S"item"] };
        t2->PrimaryKey = SecondColumn;

        // Merge the table into the DataSet.
        Console::WriteLine(S"Merging...");
        ds->Merge(t2, false, System::Data::MissingSchemaAction::Add);
    };

    static void Merge_Failed(Object* sender, System::Data::MergeFailedEventArgs* e) {
        Console::WriteLine(S"Merge_Failed Event: ' {0}'", e->Conflict);
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DataSet Class | DataSet Members | System.Data Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft