• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 744
  • Last Modified:

VB 2008 - saving master/detail data in a transaction

Dear Experts,
Am trying to save data in a master/detail form using transaction scope and not been sucesful.

Have a look at my codebelow and advise.
Try
            Using scope As TransactionScope = New TransactionScope
                Me.Validate()
                Me.DespatchDateDateTimePicker.DataBindings("value").WriteValue()
                Me.TransDateDateTimePicker.DataBindings("value").WriteValue()
                Me.GardenInvoiceHdrBindingSource.EndEdit()
                Me.GardenInvoiceHdrTableAdapter.Update(Me.GardenInvDataSet.GardenInvoiceHdr)
                Me.Validate()
                Me.GardenInvoiceDtlBindingSource.Position = Me.GardenInvoiceHdrBindingSource.Position

                Me.GardenInvoiceDtlBindingSource.EndEdit()
                Me.GardenInvoiceDtlTableAdapter.Update(Me.GardenInvDataSet.GardenInvoiceDtl)
                scope.Complete()
            End Using
            MessageBox.Show("Record Saved", "Save record", MessageBoxButtons.OK, MessageBoxIcon.Information)
            GroupBox2.Enabled = True
        Catch ex As TransactionAbortedException
            MessageBox.Show(ex.Message, "Tea Tracking | Save Record-Transaction Aborted", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex1 As ApplicationException
            MessageBox.Show(ex1.Message, "Tea Tracking | Save Record-Application exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex2 As Exception
            MessageBox.Show(ex2.Message, "Tea Tracking | Save Record", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Open in new window

0
Norman Maina
Asked:
Norman Maina
  • 4
  • 2
1 Solution
 
CodeCruiserCommented:
>and not been sucesful.

Any error?
0
 
Norman MainaAuthor Commented:
Am getting a constraints exception.
The  keys referenced in the exception are the primary key of the master table and the foreign key or the details table.

In MS reference, it states all I have to do is call the bindingsources endedit of the parent then update the parent,then call the endedit of the child bindingsource before updating.
This is what am doing and getting this exeption -dont know what am doing wrong.
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_GardenInvoiceDtl_GardenInvoiceHdr". The conflict occurred in database "TeaTracking", table "dbo.GardenInvoiceHdr", column 'GardenID'. The statement has been terminated.

Open in new window

0
 
Norman MainaAuthor Commented:
what is ive been used to doing is force user to saving the master first and then save the child after the master records are in.

But i know theres a more elegant way of doing this -including the use of transactions so that if entry of master fails-entry of child fails too.

Any ideas would be appreciated.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
CodeCruiserCommented:
If there is an error when saving master, an exception will be generated and your call to update child table will not be executed anyway.
0
 
Norman MainaAuthor Commented:
But is I save the master only,it will save and then i can save the details.
But if I want to save them together sequentially,its not working...master first and then details to follow.
0
 
Norman MainaAuthor Commented:
no other answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now