LINQ Transactions

To use Transaction in LINQ

initial steps from http://vinodkotiya.blogspot.com/2011/07/binding-gridview-with-linq-sql-data.html

Add system.transactionscope dll in Addreference > .Net Tab

Imports System.Linq

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        'connecting with DBML
        Dim d As DataClassesDataContext = New DataClassesDataContext
        Dim t = (From d1 In d.vins Select d1).First

        If (t.eid > 100) Then
            t.name = "update1"
        End If

        '1st method
        ' Using ts As transactionscope = New transactionscope
        'd.SubmitChanges()
        'ts.complete()
        ' End Using

        ''2nd method for dataset
        d.Connection.Open()
        d.Transaction = d.Connection.BeginTransaction
        Try
            d.SubmitChanges()
            d.Transaction.Commit()
        Catch ex As Exception
            d.Transaction.Rollback()

        Finally
            d.Transaction = Nothing
        End Try

        Dim g1 = From g2 In d.vins Select g2
        GridView1.DataSource = g1
        GridView1.DataBind()
       
    End Sub

- Vinod Kotiya

Comments