Working with Transaction

At times when you are inserting records into the database involving more than one table, it is possible that some errors occurs and records is not properly inserted. To prevent the damages, one needs define the transactions. So this is how I defined the transaction

Dim connection As New SqlClient.SqlConnection(Common.GetConnectionString)
connection.Open()
Dim trans As SqlClient.SqlTransaction

Try

Dim dtDND As New DataTable
dtDND = CType(ViewState("getDND"), DataTable)
'dtDND = SCMERP.Sale.getDND(tbxFromDate.Text, tbxToDate.Text)
Dim ClaimID As String

trans = connection.BeginTransaction
Dim DocumentNo As String
DocumentNo = SCMERP.Purchase.GetDocumentNumber(Common.DocumentTypes.Sale, Common.BranchID)
If ddlStockType.SelectedValue = Common.StockType.DND Then

ClaimID = SCMERP.Purchase.SetDocument(trans, 10, DocumentNo, "12/12/2008", Common.BranchID, Common.PrincipalID.Principal, dtDND.Rows(0)("Total"), dtDND.Rows(0)("Tax"), dtDND.Rows(0)("Discount"), dtDND.Rows(0)("Amount"), Nothing, 0, 0, 0, 0, 0, Nothing, 0, "", False, Nothing, 0, 0, Nothing, Nothing, 0.0, 0.0, 0, "", 0, 0, "", 0, tbxDesc.Text)


For count As Integer = 0 To dtDND.Rows.Count - 1
SCMERP.Purchase.SetDocumentProducts(trans, ClaimID, dtDND.Rows(count)("PriceListProductID"), dtDND.Rows(count)("DPQuantity"), dtDND.Rows(count)("DPTotal"), dtDND.Rows(count)("DPTax"), dtDND.Rows(count)("DPDiscount"), dtDND.Rows(count)("DPAmount"), Common.StockType.ClaimIn, 0, 0)


Next
trans.Commit()

ElseIf ddlStockType.SelectedValue = Common.StockType.Scheme Then
Dim dtScheme As New DataTable
ClaimID = SCMERP.Sale.ClaimInsert(tbxFromDate.Text, tbxToDate.Text, 0, tbxDesc.Text)

dtScheme = SCMERP.Sale.getScheme(tbxFromDate.Text, tbxToDate.Text, ddlScheme.SelectedValue)
For count As Integer = 0 To dtScheme.Rows.Count - 1
SCMERP.Sale.ClaimProductsInsert(ClaimID, dtScheme.Rows(count)("DocumentProductID"), dtScheme.Rows(count)("PackingID"), dtScheme.Rows(count)("BrandID"), dtScheme.Rows(count)("Quantity"))

Next


End If
RowFooter.Visible = False
RowHeader.Visible = False

Catch ex As Exception

trans.Rollback()
connection.Close()
End Try
End Sub



Comments

Popular posts from this blog

SPFx: Develop using SharePoint Framework without Installing all the dependecies.

SharePoint Online: Elevated Permissions....with love

Powershell: Filling up an Existing Excel Sheet with data from SQL Server