Microsoft Knowledge Base Email Alertz

KBAlertz.com: (310351) - This article describes how to roll back updates after an error when you use a DataAdapter and a DataSet object. If your client application uses an ADO.NET DataSet object to store changes that are made to data or to add new records, you can use a...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 310351 - Last Review: June 29, 2004 - Revision: 2.2

How To Roll Back Updates After an Error When You Use DataAdapter and DataSet in ADO.NET and Visual Basic .NET

This article was previously published under Q310351

On This Page

SUMMARY

This article describes how to roll back updates after an error when you use a DataAdapter and a DataSet object.

If your client application uses an ADO.NET DataSet object to store changes that are made to data or to add new records, you can use a Transaction object, a DataSet object, and a DataAdapter object to roll back any changes if an error occurs. If you use a CommandBuilder object to generate INSERT, UPDATE, and DELETE commands, you must set the Transaction property of the SELECT command of the DataAdapter.

If you use a wizard to generate a typed DataSet and its Command objects to populate the InsertCommand, the UpdateCommand, and the DeleteCommand properties of the DataAdapter, you must set the Transaction property for each InsertCommand, UpdateCommand, and DeleteCommand but not the SelectCommand of the DataSet.

NOTE: This article uses the CommandBuilder.

Steps to Build the Sample

  1. Follow these steps to create a new Visual Basic .NET Windows Application project:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
  2. Add the following Imports statement to the "Form1 Declarations" section:
    Imports System.Data.SqlClient
    					
  3. Add a Button control, and then add the following code to the Click event of Button1:
    Dim stConnect As String = "user id=myUserID;password=myPassword;initial catalog=northwind;data source=myServer;Connect Timeout=30"
            Dim cnNorthwind As New SqlConnection(stConnect)
            Dim stSQL As String = "select * from employees"
            Dim cmNorthwind As New SqlCommand(stSQL, cnNorthwind)
            Dim daNorthwind As New SqlDataAdapter(cmNorthwind)
            'sqlCommandBuilder will generate the insert, update and delete command of the DataAdapter
            Dim cmbNorthwind As New SqlCommandBuilder(daNorthwind)
            Dim dsNorthwind As New DataSet()
            Dim trNorthwind As SqlTransaction
            daNorthwind.Fill(dsNorthwind, "Employees")
    
            cnNorthwind.Open()
            
    
    
            ' Add you code to add or change a row(s) here
            'Dim dr As DataRow
            'dr = dsNorthwind.Tables(0).NewRow
            'dr(1) = "John"
            'dr(2) = "Doe"
            'dsNorthwind.Tables(0).Rows.Add(dr)
    
            Dim dsNorthwind2 As New DataSet()
            dsNorthwind2 = dsNorthwind.GetChanges
            trNorthwind = cnNorthwind.BeginTransaction(IsolationLevel.ReadCommitted)
    
            daNorthwind.SelectCommand.Transaction = trNorthwind
    
            Try
                daNorthwind.Update(dsNorthwind2, "Employees")
                trNorthwind.Commit()
                ' if you expect ds2 to be modified by the Update process
                dsNorthwind.Merge(dsNorthwind2, False)
    
                MsgBox("Record(s) add or changed")
            Catch ex As Exception
                Try
                    trNorthwind.Rollback()
                    MsgBox("An error accured and all changes have been Rollback")
                Catch TransacationEx As Exception
                    'Handle Exception
    
                End Try
    
            End Try
    					
  4. Modify the parameter of the connection string property of the SqlConnection object as necessary for your environment.
  5. Run the code, and note that the changes have been rolled back. You can uncomment the code that adds a DataRow to see the transaction committed.
  6. You can add a DataGrid control and code to bind the grid to your DataSet to view records, to make changes, or to add records to the DataSet.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
SqlTransaction Class
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.aspx (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.aspx)

OleDbTransaction Class
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtransaction.aspx (http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtransaction.aspx)

How to Add a DataRow to a DataTable in a DataSet
http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx (http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx)

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
Keywords: 
kbdataadapter kbhowtomaster kbpending kbsqlclient kbsystemdata KB310351
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please