MergeFailed event

G

Gene

I am having problems getting the MergeFailed event to fire using the
following code:

' Fill a DataSet with data from the pubs database
Dim dbConn As New
SqlClient.SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
Dim da As New SqlClient.SqlDataAdapter("SELECT au_id,
au_lname, au_fname, contract FROM authors", dbConn)
Dim ds As New DataSet("DS")
Dim ds2 As New DataSet("DS2")

' Fill with data and schema
da.Fill(ds)

' Only the schema for the other dataset
da.FillSchema(ds2, SchemaType.Source)

' Print the starting number of rows
Console.WriteLine("Starting number of rows: {0}",
ds.Tables(0).Rows.Count)

' Add a new row
Dim r As DataRow
r = ds2.Tables(0).NewRow()
r("au_id") = "172-32-1176"
r("au_fname") = "Fabio Claudio"
r("au_lname") = "Ferracchiata"
r("contract") = True
ds2.Tables(0).Rows.Add(r)

' Add the event handler to manage merge failures
AddHandler ds.MergeFailed, New
MergeFailedEventHandler(AddressOf OnMergeFailed)

' Merge the two DataSets
ds.Merge(ds2)

Private Sub OnMergeFailed(ByVal sender As Object, ByVal args As
MergeFailedEventArgs)
' Manage the errors here
Console.WriteLine("Merge failed for table " &
args.Table.TableName)
Console.WriteLine("Conflict = " & args.Conflict)
End Sub

The merge does fail since au_id = 172-32-1176 is already in the
authors table, but the OnMergeFailed() event handler is never called.

In the book "Microsoft ADO.NET Core Reference", David Sceppa, the
author, states "Personally, I've been unable to cause the MergeFailed
event to fire".

Is this a bug? Has anyone been able to get the MergeFailed event to
fire?
 
W

William Ryan

Excellent book..it's been a life saver.

I've had the same problem for what it's worth and I'm I
don't see anything in any documentation to indicate the
it only fires under some specific condition unrelated to
an explicit call.

I've chalked it up to a bug for the time being (then
again, I've done that before and the bug was usually
mine ;-) ) .

I'd be interested in what MS has to say about it.

Bill

Cordially,

W.G. Ryan
(e-mail address removed)
www.knowdotnet.com
 
S

Scot Rose [MSFT]

Hi William, I have been fighting with this all morning and I cannot get it to fire either (I have a C# sample that DOES fire it, but creating a similar VB sample does not)... Will let you
know what I find out as I dig into this more...

In case you are interested here is the C# Code, if you can see what we may be missing let me know<G>

private static void DemonstrateMergeFailedEvent()
{
// create a DataSet with one table, two columns, and ten rows.
DataSet ds = new DataSet("myDataSet");
DataTable t = new DataTable("Items");

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

// create and add two columns to the DataTable
// add the column and set it's AutoIncrement property
t.Columns.Add( "id", typeof(int) ).AutoIncrement = true;
// add the second column
t.Columns.Add( "item", typeof(int) );

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

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

// add ten rows.
DataRow r;
for( int i=0; i<10; i++ )
{
r = t.NewRow();
r["Item"] = i;
t.Rows.Add( r );
}

// accept changes.
ds.AcceptChanges();
PrintValues( ds, "Original values" );

// create a second DataTable identical to the first,
// using the Clone method and add an extra column
DataTable t2 = t.Clone();
t2.Columns.Add( "extra", typeof( string ) );

// add four rows, note that duplicate id column values
// will fire the MergeFailed event
t2.Rows.Add( new Object[] { 12, 555, "extra column 1" } );
t2.Rows.Add( new Object[] { 13, 665, "extra column 2" } );
t2.Rows.Add( new Object[] { 1, 774, "extra column 3" } );
t2.Rows.Add( new Object[] { 2, 883, "extra column 4" } );
t2.PrimaryKey = new DataColumn[1] { t2.Columns["item"] };

// merge the table into the DataSet.
Console.WriteLine( "Merging..." );
ds.Merge( t2, false, MissingSchemaAction.Add );
// instruct the DataSet to enforce constraints

//PrintValues(ds, "Merged With Table, Schema Added");
}

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

private static void PrintValues( DataSet ds, string label )
{
Console.WriteLine();
Console.WriteLine( "{0}\n", label );
foreach( DataTable t in ds.Tables )
{
Console.WriteLine( "TableName: {0}", t.TableName );
foreach( DataRow r in t.Rows )
{
foreach( DataColumn c in t.Columns )
{
Console.Write( "\t {0}", r[c] );
}
Console.WriteLine();
}
}
}


Want to know more? Check out the MSDN Library at http://msdn.microsoft.com or the Microsoft Knowledge Base at http://support.microsoft.com

Scot Rose, MCSD
Microsoft Visual Basic Developer Support
Email : (e-mail address removed) <Remove word online. from address>

This posting is provided “AS IS”, with no warranties, and confers no rights.




--------------------
 
S

Scot Rose [MSFT]

William, Basically what I found out is this, The Merge was not failing, a ROW in teh merge failed but teh Merge itself does not (Hence why it doesn't fire when Some of the rows are
updated in my tests)... The Merge completed Successfully even if No Rows were merged...

Want to know more? Check out the MSDN Library at http://msdn.microsoft.com or the Microsoft Knowledge Base at http://support.microsoft.com

Scot Rose, MCSD
Microsoft Visual Basic Developer Support
Email : (e-mail address removed) <Remove word online. from address>

This posting is provided “AS IS”, with no warranties, and confers no rights.




--------------------
X-Tomcat-ID: 401986407
References: <[email protected]> <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: (e-mail address removed) (Scot Rose [MSFT])
Organization: Microsoft
Date: Fri, 08 Aug 2003 16:25:42 GMT
Subject: RE: MergeFailed event
X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
Message-ID: <SFSg#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.adonet
Lines: 161
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:57840
NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122

Hi William, I have been fighting with this all morning and I cannot get it to fire either (I have a C# sample that DOES fire it, but creating a similar VB sample does not)... Will let you
know what I find out as I dig into this more...

In case you are interested here is the C# Code, if you can see what we may be missing let me know<G>

private static void DemonstrateMergeFailedEvent()
{
// create a DataSet with one table, two columns, and ten rows.
DataSet ds = new DataSet("myDataSet");
DataTable t = new DataTable("Items");

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

// create and add two columns to the DataTable
// add the column and set it's AutoIncrement property
t.Columns.Add( "id", typeof(int) ).AutoIncrement = true;
// add the second column
t.Columns.Add( "item", typeof(int) );

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

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

// add ten rows.
DataRow r;
for( int i=0; i<10; i++ )
{
r = t.NewRow();
r["Item"] = i;
t.Rows.Add( r );
}

// accept changes.
ds.AcceptChanges();
PrintValues( ds, "Original values" );

// create a second DataTable identical to the first,
// using the Clone method and add an extra column
DataTable t2 = t.Clone();
t2.Columns.Add( "extra", typeof( string ) );

// add four rows, note that duplicate id column values
// will fire the MergeFailed event
t2.Rows.Add( new Object[] { 12, 555, "extra column 1" } );
t2.Rows.Add( new Object[] { 13, 665, "extra column 2" } );
t2.Rows.Add( new Object[] { 1, 774, "extra column 3" } );
t2.Rows.Add( new Object[] { 2, 883, "extra column 4" } );
t2.PrimaryKey = new DataColumn[1] { t2.Columns["item"] };

// merge the table into the DataSet.
Console.WriteLine( "Merging..." );
ds.Merge( t2, false, MissingSchemaAction.Add );
// instruct the DataSet to enforce constraints

//PrintValues(ds, "Merged With Table, Schema Added");
}

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

private static void PrintValues( DataSet ds, string label )
{
Console.WriteLine();
Console.WriteLine( "{0}\n", label );
foreach( DataTable t in ds.Tables )
{
Console.WriteLine( "TableName: {0}", t.TableName );
foreach( DataRow r in t.Rows )
{
foreach( DataColumn c in t.Columns )
{
Console.Write( "\t {0}", r[c] );
}
Console.WriteLine();
}
}
}


Want to know more? Check out the MSDN Library at http://msdn.microsoft.com or the Microsoft Knowledge Base at http://support.microsoft.com

Scot Rose, MCSD
Microsoft Visual Basic Developer Support
Email : (e-mail address removed) <Remove word online. from address>

This posting is provided “AS IS”, with no warranties, and confers no rights.




--------------------
Content-Class: urn:content-classes:message
From: "William Ryan" <[email protected]>
Sender: "William Ryan" <[email protected]>
References: <[email protected]>
Subject: MergeFailed event
Date: Thu, 7 Aug 2003 12:30:27 -0700
Lines: 86
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNdGlrhckto3xOySkKTEw5+Gm2C1A==
Newsgroups: microsoft.public.dotnet.framework.adonet
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:57725
NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
X-Tomcat-NG: microsoft.public.dotnet.framework.adonet

Excellent book..it's been a life saver.

I've had the same problem for what it's worth and I'm I
don't see anything in any documentation to indicate the
it only fires under some specific condition unrelated to
an explicit call.

I've chalked it up to a bug for the time being (then
again, I've done that before and the bug was usually
mine ;-) ) .

I'd be interested in what MS has to say about it.

Bill

Cordially,

W.G. Ryan
(e-mail address removed)
www.knowdotnet.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top