Working with DataSet Events in ADO.NET

This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
 
A dataset has a MergeFailed event that occurs when merging two datasets fails. MergeFailedEventHandler handles the MergeFailed event, which receives an the argument of type MergeFailedEventArges that contains data related to this event. The MergeFailedEventHandler is as follows:
  1. public delegate void MergeFailedEventHandler(object sender, MergeFailedEventArgs e); 
Where the sender is the source of the event and e is the MergeFailedEventArgs abject that contains the event data.
 
The 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 9-7 shows an example of the MergeFailed event handler. As you can see, the MergeFailedBtn_Click method creates a connection and a data adapter and then fills a dataset using the Fill method of the data adapter. After that, the code creates a second dataset, calls the Fill method to fill it, and then calls the Merge method of the dataset.
 
Listing 9-7. Writing code to call the MergeFailed event handler
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using System.Data.OleDb;  
  11.   
  12. namespace Handling_ADO.NET_Events {  
  13.     public partial class Form1: Form {  
  14.         public Form1() {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private void MergeFailedBtn_Click(object sender, System.EventArgs e) {  
  19.             OleDbConnection conn = new OleDbConnection();  
  20.             string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" +  
  21.                 "Data Source=C:\\Northwind.mdb";  
  22.             conn.ConnectionString = strDSN;  
  23.             conn.Open();  
  24.             string sql = "SELECT * FROM Employees";  
  25.             OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);  
  26.             DataSet ds1 = new DataSet("ds1");  
  27.             da.Fill(ds1);  
  28.             sql = "SELECT * FROM Customers";  
  29.             da = new OleDbDataAdapter(sql, conn);  
  30.             DataSet ds2 = new DataSet("ds2");  
  31.             da.Fill(ds2);  
  32.             ds1.MergeFailed += new MergeFailedEventHandler(OnMergeFailed);  
  33.             ds1.Merge(ds2);  
  34.         }  
  35.   
  36.         protected static void OnMergeFailed(object sender, MergeFailedEventArgs args) {  
  37.             MessageBox.Show(args.Conflict.ToString());  
  38.         }  
  39.     }  

Conclusion

 
Hope this article would have helped you in understanding working with DataSet Events in ADO.NET. See other articles on the website also for further reference.
 
adobook.jpg
 
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.