ARTICLE

Working with DataSet Events in ADO.NET

Posted by Dinesh Beniwal Articles | ADO.NET in VB.NET May 26, 2010
In this article I will explain working with DataSet Events in ADO.NET.
 
Reader Level:

HTML clipboard

A dataset has a MergeFailed event that occurs when merging two datasets fails. MergeFailedEventHandler handles the MergeFailed event, which receives an argument of type MergeFailedEventArges that contains data related to this event. The MergeFailedEventHandler is as follows:

Public Delegate Sub MergeFailedEventHandler(ByVal sender As Object, ByVal e As MergeFailedEventArgs)

Where 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



Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Data.OleDb

Namespace Handling_ADO.NET_Events

    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub MergeFailedBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim conn As New OleDbConnection()
            Dim strDSN As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Northwind.mdb"
 
            conn.ConnectionString = strDSN

            conn.Open()

            Dim sql As String = "SELECT * FROM Employees"
 
            Dim da As New OleDbDataAdapter(sql, conn)
            Dim ds1 As New DataSet("ds1")
 
            da.Fill(ds1)
 
            sql = "SELECT * FROM Customers"
 
            da = New OleDbDataAdapter(sql, conn)
 
            Dim ds2 As New DataSet("ds2")
 
            da.Fill(ds2)

            AddHandler ds1.MergeFailed, AddressOf OnMergeFailed
            ds1.Merge(ds2)
        End Sub
 
        Protected Shared Sub OnMergeFailed(ByVal sender As Object, ByVal args As MergeFailedEventArgs)
            MessageBox.Show(args.Conflict.ToString())

    End Class
End Namespace

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor