473,513 Members | 11,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1999

"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
3069
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
1686
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
276
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
3518
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
5553
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
1622
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
3647
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
18313
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
2994
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
7373
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,...
1
7094
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
5677
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,...
1
5079
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...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.