473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

DataSets, RowStates and Transactions

I have a DataTable that I am updating inside a transaction

After each row is updated the changes on the row are implicitly accepted by
the dataAdapter and the rowstate is set to unchanged.
However If the transaction fails - Say, because a unique constraint is
violated, the transaction rolls back but the rowstate is not rolled back ant
the dataset is nolonger in sync with the database. This means that after the
errors are fixed and the update is called again, the rows which were updated
successfully first time are not updated again.

Can I Avoid this behaviour and have the rowstate rolled back with the
transaction?
'=======================================
'Assocciate comands with Data Adapter
With _DataAdapter
.InsertCommand = _InsertCommand
.UpdateCommand = _UpdateCommand
.DeleteCommand = _DeleteCommand
End With

'Asign a transaction to the commands
Dim _Transaction As SqlClient.SqlTransaction =
_Connection.BeginTransaction
_UpdateCommand.Transaction = _Transaction
_InsertCommand.Transaction = _Transaction
_DeleteCommand.Transaction = _Transaction

Try
_DataAdapter.Update(DocTypes)
_Transaction.Commit()
Catch ex As DBConcurrencyException When
ex.Message.IndexOf("affected 0 records.") > -1
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message)
Catch ex As Data.SqlClient.SqlException When
ex.Message.IndexOf("Violation of PRIMARY KEY constraint") > -1
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message)
Catch ex As Data.SqlClient.SqlException When
ex.Message.IndexOf("Violation of UNIQUE KEY constraint") > -1
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message)
Catch ex As Exception
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message)
Finally
_Connection.Close()
End Try
'================================================
Nov 22 '05 #1
2 1779
Not until the next version of ADO.NET comes out which has a
AcceptChangesDuringUpdate property. This behavior is actually by design and
probably the best solution is this - Create another dataset and use the
GetChanges Method of the dataset that you are currently passing to the
adapter. Now, pass the new dataset to the adapter. If everything goes as
planned, call .AcceptChanges on the Original Dataset in in the Same place
you call Commit for the transaction. Otherwise, just get rid of the dataset
that was holding the changes. This way if the transaction is successful,
then the original dataset's changes will be in the database correctly and
the rowstates will be what they would have been, but if not, then you still
have the dataset's rowchanges. You can of course use GetChanges and pass
the original to the adapter and use the same logic just in reverse - either
way you get to the same place.

HTH,

Bill

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Ben Reese" <Be******@discussions.microsoft.com> wrote in message
news:69**********************************@microsof t.com...
I have a DataTable that I am updating inside a transaction

After each row is updated the changes on the row are implicitly accepted by the dataAdapter and the rowstate is set to unchanged.
However If the transaction fails - Say, because a unique constraint is
violated, the transaction rolls back but the rowstate is not rolled back ant the dataset is nolonger in sync with the database. This means that after the errors are fixed and the update is called again, the rows which were updated successfully first time are not updated again.

Can I Avoid this behaviour and have the rowstate rolled back with the
transaction?
'=======================================
'Assocciate comands with Data Adapter
With _DataAdapter
.InsertCommand = _InsertCommand
.UpdateCommand = _UpdateCommand
.DeleteCommand = _DeleteCommand
End With

'Asign a transaction to the commands
Dim _Transaction As SqlClient.SqlTransaction =
_Connection.BeginTransaction
_UpdateCommand.Transaction = _Transaction
_InsertCommand.Transaction = _Transaction
_DeleteCommand.Transaction = _Transaction

Try
_DataAdapter.Update(DocTypes)
_Transaction.Commit()
Catch ex As DBConcurrencyException When
ex.Message.IndexOf("affected 0 records.") > -1
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message) Catch ex As Data.SqlClient.SqlException When
ex.Message.IndexOf("Violation of PRIMARY KEY constraint") > -1
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message) Catch ex As Data.SqlClient.SqlException When
ex.Message.IndexOf("Violation of UNIQUE KEY constraint") > -1
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message) Catch ex As Exception
_Transaction.Rollback()
Throw New BR.LookupData.DatabaseUpdateException(ex.Message) Finally
_Connection.Close()
End Try
'================================================

Nov 22 '05 #2
Thank you

This worked.
I loose the nice row errors that contain the SQL Error message but I can
work around that.

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard Maher | last post by:
Hi, I am seeking the help of volunteers to test some software that I've developed which facilitates distributed two-phase commit transactions, encompassing any resource manager (e.g. SQL/Server...
0
by: Natehop | last post by:
I've been attempting to design an n-tiered framework leveraging .NET's strongly typed Dataset. My Framework will serve as the foundation to several client apps from Windows applications to web...
2
by: Ben Reese | last post by:
I have a DataTable that I am updating inside a transaction After each row is updated the changes on the row are implicitly accepted by the dataAdapter and the rowstate is set to unchanged....
2
by: Grant | last post by:
Hello, Ive got a recordset containing a table. I add a new row with 2 of the columns populated, and then add the row to the datatable - Dataset.Datatable.Rows.Add(newrow) I then fill in the...
2
by: codejockey | last post by:
I have a simple project that requires I take a set of data from an Excel spreadsheet, compare it to a table in SQL Server (where column names match), and if there are changes in the Excel sheet,...
0
by: codejockey | last post by:
Please forgive the repost, but I'm trying to avoid the hack I want to implement since I cant get this sample to work. Can anyone help? *********************** William: Thanks for the reply. I...
11
by: Mike P | last post by:
I've been using C# transactions for a while and had no problems with them. Using try catch blocks I can trap basically all possible errors and rollback all necessary data. Over the last few...
4
by: S Chapman | last post by:
Is there any tool that generates Typed DataTables rather than Typed Datasets? The trouble we are having is we have quite a few tables in our database and a single table can be a part of more than...
12
by: Rami | last post by:
I have some requirement for an automated payment system. The system has four machines setup as follows: 1- Two machines have a clustered database. 2- Two machines have a .net business logic...
12
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.