Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Working with DataSet Events in ADO.NET
WhatsApp
Mahesh Chand
4y
17.9k
0
1
100
Article
Working with DataSet Events.zip
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:
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
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
using
System.Data.OleDb;
namespace
Handling_ADO.NET_Events {
public
partial
class
Form1: Form {
public
Form1() {
InitializeComponent();
}
private
void
MergeFailedBtn_Click(
object
sender, System.EventArgs e) {
OleDbConnection conn =
new
OleDbConnection();
string
strDSN =
"Provider=Microsoft.Jet.OLEDB.4.0;"
+
"Data Source=C:\\Northwind.mdb"
;
conn.ConnectionString = strDSN;
conn.Open();
string
sql =
"SELECT * FROM Employees"
;
OleDbDataAdapter da =
new
OleDbDataAdapter(sql, conn);
DataSet ds1 =
new
DataSet(
"ds1"
);
da.Fill(ds1);
sql =
"SELECT * FROM Customers"
;
da =
new
OleDbDataAdapter(sql, conn);
DataSet ds2 =
new
DataSet(
"ds2"
);
da.Fill(ds2);
ds1.MergeFailed +=
new
MergeFailedEventHandler(OnMergeFailed);
ds1.Merge(ds2);
}
protected
static
void
OnMergeFailed(
object
sender, MergeFailedEventArgs args) {
MessageBox.Show(args.Conflict.ToString());
}
}
}
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.
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.
ADO.NET
DataSet Events
MergeFailed event handler
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.4k people
Download Now!
Learn
View all
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.
Membership not found