473,402 Members | 2,046 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,402 software developers and data experts.

Need help (Update with transaction)

Hi,
I have 1 dataset with 2 tables (Table1 as parent, Table2 as Child), 1 row in
both the tables.
I am updating it with a transaction. First parent then child.
When child update fails, it raise an exception, and rollback the parent
update. But it also change the rowstate of the parent row from Added to
unchanged
When you try to save again parent row never get created in database. So
child update will fail again.
Please suggest what am I suppose to do to update both parent and child.
This is what I am doing. This is just a SAMPLE code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim sTransaction As SqlClient.SqlTransaction
SqlConnection1.Open()
sTransaction = SqlConnection1.BeginTransaction
Try
daTable1.InsertCommand.Transaction = sTransaction
daTable1.UpdateCommand.Transaction = sTransaction
daTable1.DeleteCommand.Transaction = sTransaction
daTable1.Update(ds)

daTable2.InsertCommand.Transaction = sTransaction
daTable2.UpdateCommand.Transaction = sTransaction
daTable2.DeleteCommand.Transaction = sTransaction
daTable2.Update(ds)
sTransaction.Commit()
Catch ex As Exception
sTransaction.Rollback()
MsgBox(ex.Message)
Finally
SqlConnection1.Close()
End Try
End Sub

Please advise me.Thanks in advance,
RC

Nov 21 '05 #1
2 1991

"Richard" <kg@nospam.com> wrote in message
news:eR**************@TK2MSFTNGP14.phx.gbl...
Hi,
I have 1 dataset with 2 tables (Table1 as parent, Table2 as Child), 1 row
in both the tables.
I am updating it with a transaction. First parent then child.
When child update fails, it raise an exception, and rollback the parent
update. But it also change the rowstate of the parent row from Added to
unchanged
The DataSet is not transactional, so you need to preserve a copy of your
DataSet's before state in order to restore it after a rollback.

One way to do this is to use DataSet.GetChanges() to get a new DataSet
containing only the changes to your main DataSet. Use the changes DataSet
for the DataAdapter, and then, only on success, run ds.AcceptChanges.

A caviat here is that if the update procedure generates any changes to the
DataSet, you would have to marshal them back to the original DataSet. In
the case of IDENTITY columns, this can be a pain.

Another option is to make a complete copy of your DataSet, attempt the
update, and if it fails, replace your DataSet with the pre-update copy.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim sTransaction As SqlClient.SqlTransaction
SqlConnection1.Open()
sTransaction = SqlConnection1.BeginTransaction
Try
daTable1.InsertCommand.Transaction = sTransaction
daTable1.UpdateCommand.Transaction = sTransaction
daTable1.DeleteCommand.Transaction = sTransaction dim changes as DataSet = ds.GetChanges() daTable1.Update(changes)

daTable2.InsertCommand.Transaction = sTransaction
daTable2.UpdateCommand.Transaction = sTransaction
daTable2.DeleteCommand.Transaction = sTransaction
daTable2.Update(changes)
'marshal any db-originated updates back to ds
ds.AcceptChanges sTransaction.Commit()
Catch ex As Exception 'nothing to do to ds since no changes have been applied.
sTransaction.Rollback()
MsgBox(ex.Message)
Finally
SqlConnection1.Close()
End Try
End Sub


David
Nov 21 '05 #2

"Richard" <kg@nospam.com> wrote in message
news:eR**************@TK2MSFTNGP14.phx.gbl...
Hi,
I have 1 dataset with 2 tables (Table1 as parent, Table2 as Child), 1 row
in both the tables.
I am updating it with a transaction. First parent then child.
When child update fails, it raise an exception, and rollback the parent
update. But it also change the rowstate of the parent row from Added to
unchanged
The DataSet is not transactional, so you need to preserve a copy of your
DataSet's before state in order to restore it after a rollback.

One way to do this is to use DataSet.GetChanges() to get a new DataSet
containing only the changes to your main DataSet. Use the changes DataSet
for the DataAdapter, and then, only on success, run ds.AcceptChanges.

A caviat here is that if the update procedure generates any changes to the
DataSet, you would have to marshal them back to the original DataSet. In
the case of IDENTITY columns, this can be a pain.

Another option is to make a complete copy of your DataSet, attempt the
update, and if it fails, replace your DataSet with the pre-update copy.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim sTransaction As SqlClient.SqlTransaction
SqlConnection1.Open()
sTransaction = SqlConnection1.BeginTransaction
Try
daTable1.InsertCommand.Transaction = sTransaction
daTable1.UpdateCommand.Transaction = sTransaction
daTable1.DeleteCommand.Transaction = sTransaction dim changes as DataSet = ds.GetChanges() daTable1.Update(changes)

daTable2.InsertCommand.Transaction = sTransaction
daTable2.UpdateCommand.Transaction = sTransaction
daTable2.DeleteCommand.Transaction = sTransaction
daTable2.Update(changes)
'marshal any db-originated updates back to ds
ds.AcceptChanges sTransaction.Commit()
Catch ex As Exception 'nothing to do to ds since no changes have been applied.
sTransaction.Rollback()
MsgBox(ex.Message)
Finally
SqlConnection1.Close()
End Try
End Sub


David
Nov 21 '05 #3

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

Similar topics

0
by: Wil Simpson | last post by:
what is the best way to calculate: (in the context of an update) the number of transactions that were entered in the same way as the current transaction out of the 5 last transactions by the...
9
by: pankaj_wolfhunter | last post by:
Hi, I need some clearance on the following questions: 1) Does LOAD command updates indexes defined for a table? 2) Is REPLACE option in the LOAD command a logged operation? Help will be...
0
by: Richard | last post by:
Hi, I have 1 dataset with 2 tables (Table1 as parent, Table2 as Child), 1 row in both the tables. I am updating it with a transaction. First parent then child. When child update fails, it raise...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
5
by: Louis LeBlanc | last post by:
Hey folks. I'm new to the list, and not quite what you'd call a DB Guru, so please be patient with me. I'm afraid the lead up here is a bit verbose . . . I am working on an application that...
13
by: Jeff Davis | last post by:
Right now performance isn't a problem, but this question has me curious: Let's say I have a shopping cart system where there is a "products" table that contains all possible products, and an...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
0
by: gshawn3 | last post by:
Hi, I am having a hard time creating a Trigger to update an Oracle database. I am using a SQL Server 2005 Express database on a Win XP Pro SP2 desktop, linked to an Oracle 10g database on a...
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:
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.