473,386 Members | 1,785 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,386 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 1778
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.