-2

I have situation in which I need to insert a record to the DB from one form (Form1) and then open another form (Form2) in which I have to add more details to the DB and when return back to Form1. Finally, I have to open another form (Form3) from (From1) in which I have to add other details.

My requirement is if there is any failure in Form2 or Form3 the whole process should be cancelled. So all the insertion to the DB in all the forms should be rolled back. I tried implementing TransactionScope with TransactionScopeOption.Required in each of the forms but this seens to be not working the way I need it. In my case if there is a failure in Form3, the insertion in Form2 does not rollback ( I guess since the scope.Complete() is called on each scope).

Required Scenario:

  1. Form 1 Opens
  2. Insert in the DB
  3. Back to Form 1
  4. Form 2 Opens
  5. Insert in the same DB
  6. Back to Form 1
  7. Form 3 opens
  8. Failure occurs on Form 3
  9. Rollback should be called on the insertions in all forms (Form1,Form2 and Form3)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • I'd suggest figuring out a way to aggregate all the data back in some sort of collection/container populated by the other forms and then doing a single scope at the end and iterate over the data and process it – pinkfloydx33 May 16 '20 at 11:19

1 Answers1

0

If you don't post code it is difficult to give specific advice.

In principle though, what you're asking in DB terms is to be able to commit a transaction and keep your options open to change your mind in case you decide to rollback later. Not possible.

Complete() should be called only once.

To commit data between forms, consider using temp table/s and pull it all together in Form1 when done; otherwise the TransactionScope has to be obtained by Form1, held incomplete during calls to Forms2&3 and Complete()d right at the end.

G'luck

PS: @pinkfloydx33's commented suggestion for maintaining a local container between forms is excellent (better performance than DB calls, no cleanup).

AlanK
  • 1,827
  • 13
  • 16