Hello,
I tried reading as many threads as I can before posting a new one. I have the following scenario when saving a new invoice:
- Get a new invoice number from [Counter] table.
- Update the new invoice, modifying the number to show the new number.
- Save the invoice
- Update the average cost for each itemHowever, I want all of this to occur in a transaction so that if ANYTHING fails, all fails.
Here is the code I'm using now, where NOT everything is in the transaction (currently UnitOfWork):
Private Function DoSave(ByVal DisplayMessage As Boolean) As Boolean
Try
UpdateFocusedControl()
'Update the metadata
Dim x As CustomerSaleEntity = CType(Me.bindSource.Current, CustomerSaleEntity)
'Here, I'm getting the new number
If x.IsNew Then
Dim newNumber As String = GetNewNumber.GetNewNumber("CustomerSale")
x.StrCustomerSaleId = newNumber
End If
With x
.StrWarehouseId = Helpers.MainWarehouseId
.SintRecVer += 1S
.DtmDateEntered = Date.Now
.StrTerminalName = My.Computer.Name
.StrUserName = My.User.Name
.StrVer = My.Application.Info.Version.ToString
End With
'Display message when NOT saved
Dim uow As New UnitOfWork
uow.AddCollectionForDelete(Me.DeletedChildren)
uow.AddCollectionForSave(x.CustomerSaleDetail, True)
uow.AddForSave(Me.CurrentEntity, True)
If uow.Commit(New Transaction(IsolationLevel.ReadCommitted, "trans"), True) = 0 Then
If DisplayMessage = True Then
MessageBox.Show("Entry has NOT been saved")
End If
Return False
Else
Return True
End If
Catch ex As Exception
Utils.HandleException(ex, Me)
End Try
End Function
What happens now is that if the invoice fails to save, the new number is still saved and I end up with a missing invoice number.
Now, I'm thinking of using TransactionScope, but is this the right approach?