SQL Transaction in Generated DataAdapter in ASP.NET

Setiap transaksi Create, Update, dan Delete ke dalam database wajib menggunakan transaction untuk proses commit apabila proses berhasil dan melakukan rollback apabila salah satu proses gagal.

Jika kita menggunakan querystring, deklarasi sqltransaction pada umumnya biasa digunakan untuk melakukan input secara masal untuk banyak data.

Tetapi apabila kita memanfaatkan dataset dengan generated datatable dan dataadapter maka cara yang digunakan adalah menggunakan transaction scope.

SQL Transaction in Generated DataAdapter in ASP.NET


Hal yang pertama yang perlu ditambahkan adalah referensi ke System.Transactions kemudian kita perlu menambahkan Imports System.Transactions.

Dibawah ini adalah contoh penerapan transaction scope pada generated dataadapter di ASP.NET.


Protected Sub BtnSave_Click(sender As Object, e As System.EventArgs) Handles BtnSave.Click
        Try
            Dim oDA As New ESSTableAdapters.TR_DETAILTableAdapter

            Dim oDT As DataTable = ViewState("ODT")
            Dim oRB1 As RadioButton
            Dim oRB2 As RadioButton
            Dim oRB3 As RadioButton
            Dim oRB4 As RadioButton
            Dim oRB5 As RadioButton
            Dim nVal As Int16 = 0

            Using oScope As New TransactionScope()
                For i As Int16 = 0 To GvTaskList.Rows.Count - 1
                    oRB1 = GvTaskList.Rows(i).Cells(2).FindControl("RB1")
                    oRB2 = GvTaskList.Rows(i).Cells(2).FindControl("RB2")
                    oRB3 = GvTaskList.Rows(i).Cells(2).FindControl("RB3")
                    oRB4 = GvTaskList.Rows(i).Cells(2).FindControl("RB4")
                    oRB5 = GvTaskList.Rows(i).Cells(2).FindControl("RB5")

                    If oRB1.Checked = True Then
                        nVal = 1
                    End If

                    If oRB2.Checked = True Then
                        nVal = 2
                    End If

                    If oRB3.Checked = True Then
                        nVal = 3
                    End If

                    If oRB4.Checked = True Then
                        nVal = 4
                    End If

                    If oRB5.Checked = True Then
                        nVal = 5
                    End If

                    oDA.Insert(G_Periode, "name", "address", oDT.Rows(i)("QID"), nVal, Date.Now)
                Next


                'Commit
                oScope.Complete()
            End Using

        Catch ex As Exception
            InfoDiv.InnerHtml = ex.Message
        End Try
    End Sub


Setelah semua transaksi insert dilakukan maka diakhir proses kita perlu memanggil prosedur complete untuk melakukan commit.

Jika prosedur commit tidak pernah dipanggil maka setelah selesai eksekusi prosedur secara otomatis akan dianggap rollback,

Semoga posting tentang SQL Transaction in Generated DataAdapter in ASP.NET diatas dapat bermanfaat.



Salam,

Popular posts from this blog

SmartObject property ID is a required property for selected method Create. Value must be set.

Cara inject USB 3.0 Driver pada instalasi Windows - How to Inject USB 3.0 Driver in Windows 7

Python Font Color in Console