TransactionScope

Last post 10-23-2007 2:05 PM by foreachbiscuit. 1 replies.

Sort Posts:

  • TransactionScope

    10-20-2007, 5:00 PM
    • Loading...
    • cb3431
    • Joined on 01-26-2005, 3:27 PM
    • Posts 278

     I am at a loss here.

        Sub Main()
            Dim sqlConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("db").ToString)
            Dim dbCommand As New SqlCommand("test", sqlConnection)
            dbCommand.CommandType = CommandType.StoredProcedure


            Using ts As New TransactionScope
                Using sqlConnection
                    sqlConnection.Open()
                    Try
                        dbCommand.ExecuteNonQuery()
                        Console.WriteLine("Success")
                    Catch ex As SqlException
                        Console.WriteLine(ex.Message)
                    Finally
                        dbCommand.Dispose()
                    End Try
                    ts.Complete()
                End Using

            End Using

            Console.ReadKey()

        End Sub

     
    BEGIN
        Insert    dbo.Table_2
            (Column1)
            Values
            (newid())

        Insert    dbo.Table_1
            (Column1)
            Values
            ('123456')
    End

    The Try block catches the exception in the second insert statement as it should, but I still get this exception "The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION." at the last End Using. I have tried placing Begin Transaction/Commit Transaction around the insert statement, but I get the same exception. What I am doing seems to match all the examples for the TransactionScope class.


  • Re: TransactionScope

    10-23-2007, 2:05 PM
    Answer

     Try moving your Complete() outside of the Using statement for the SqlConnection:

     Using ts As New TransactionScope
                Using sqlConnection
                    sqlConnection.Open()
                    Try
                        dbCommand.ExecuteNonQuery()
                        Console.WriteLine("Success")
                    Catch ex As SqlException
                        Console.WriteLine(ex.Message)
                    Finally
                        dbCommand.Dispose()
                    End Try
                   
                End Using

                ts.Complete()
            End Using

     

    Regards,
    foreachbiscuit
    blog @ http://foreachbiscuit.wordpress.com
    Filed under:
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter