0

There is an exception thrown in the code below:

Imports System.Transactions
Imports System.Data.SqlClient

Public Class _Default
    Inherits System.Web.UI.Page

    Public Sub Test()

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Using scope As New TransactionScope()
                Test(1)
                Test(2)
                Test(3)
                Test2(4)
            End Using

        Catch ex As Exception

        End Try
    End Sub

    Public Sub Test(ByVal personID As Integer)
        Try

            Dim objCommand As New SqlCommand
            Dim strConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=AcornTest;Integrated Security=True"
            Dim objCon As New SqlConnection
            objCon.ConnectionString = strConString
            objCon.Open()
            Using objCon
                Using objCommand
                    objCommand.Connection = objCon
                    objCommand.CommandText = "UPDATE Person SET URN = @URN "
                    objCommand.Parameters.AddWithValue("@URN", personID)
                    objCommand.ExecuteNonQuery()
                End Using
            End Using
            objCon.Close()
            objCon = Nothing
        Catch ex As Exception
            'I don't swallow exceptions. 
        End Try
    End Sub

    Public Sub Test2(ByVal personID As Integer)
        Try

            Dim objCommand As New SqlCommand
            Dim strConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=AdventureWorks2008R2;Integrated Security=True"
            Dim objCon As New SqlConnection
            objCon.ConnectionString = strConString
            objCon.Open()
            Using objCon
                Using objCommand
                    objCommand.Connection = objCon
                    objCommand.CommandText = "UPDATE Person SET URN = @URN "
                    objCommand.Parameters.AddWithValue("@URN", personID)
                    objCommand.ExecuteNonQuery()
                End Using
            End Using
            objCon.Close()
            objCon = Nothing
        Catch ex As Exception
            'I don't swallow exceptions. 
        End Try
    End Sub

End Class

I thought TransactionScope allowed you to create distributed transactions. The exception is: "MSDTC on server 'IANSCOMPUTER' is unavailable." on executing Test2.objCon.Open.

Update Cserg's comment helped. I am trying to identify a bug in an enterprise application that uses SQLTransaction instances - I am moving over to TransactionScope. I am seeing this error in the log file on the test system:

The Transaction has already been implicitly or explicitly committed or aborted

I have spent five hours looking at this today with no solution. Can anyone suggest a line of enquiry?

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • 1
    `I thought TransactionScope allowed you to create distributed transactions` - it does, but did you [enable MSDTC](http://stackoverflow.com/q/7694/11683) on server 'IANSCOMPUTER'? – GSerg Aug 13 '13 at 22:20
  • 1
    http://stackoverflow.com/questions/1690892/transactionscope-automatically-escalating-to-msdtc-on-some-machines – Steve Aug 13 '13 at 22:23
  • @Cserg, thanks+1. That was it. Can you have a look at the modified question? – w0051977 Aug 13 '13 at 22:29

1 Answers1

0

I have experienced like you (MSDTC on server 'computername' is unavailable appear automatically) and try on http://geekswithblogs.net/narent/archive/2006/10/09/93544.aspx

----On the server where the trigger resides, you need to turn the MSDTC service on. You can this by clicking START > SETTINGS > CONTROL PANEL > ADMINISTRATIVE TOOLS > SERVICES. Find the service called 'Distributed Transaction Coordinator' and RIGHT CLICK (on it and select) > Start.

Its work normally.

ps: but i still don't know why MSDTC on server 'computername' is unavailable appear automatically