w0051977
w0051977

Reputation: 15817

TransactionScope error

I am seeing the following error when I attempt to open the second connection in the code below: Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool.

Public Function Test()
        Using Scope = New TransactionScope
            getMailServer()
            getMailServer()
        End Using
    End Function

    Private Function getMailServer() As String
        Dim objCommand As SqlCommand, objCommand2 As SqlCommand
        Dim objCon As SqlConnection
        Dim intDeleteCount As Integer
        Dim objDR As SqlDataReader
        Dim strServer As String
        Try
            objCommand = New SqlCommand
            objCommand2 = New SqlCommand
            objCon = New SqlConnection(_ConString) 'taken from web.config
            objCon.Open()
            objCommand.Connection = objCon
            Using objCon
                Using objCommand
                    objCommand2.Connection = objCon
                    objCommand2.CommandText = "SELECT SMTPServer FROM dbServer"
                    objDR = objCommand2.ExecuteReader
                    Do While objDR.Read
                        strServer = objDR("SMTPServer")
                    Loop
                    objDR.Close()
                End Using
            End Using
            Return strServer
        Catch ex As Exception
            Throw
        Finally

        End Try

    End Function

Please note that I have spent some time Googling this and I have tried a few things posted on this website e.g. restarting the districuted transaction coordinator in Services. I did also read somewhere that using TransactionScope should be avoided with distributed transactions (those with multiple connection objects). I am not sure if this is true.

Upvotes: 0

Views: 610

Answers (1)

zmbq
zmbq

Reputation: 39059

You can use TransactionScope for distributed transactions. In fact, by introducing TransactionScope in .NET 2.0, Microsoft put COM+ out of its misery. It was a very good move, COM+ was awful.

You need to configure DTC for network access on all machines participating in the transaction - the one running your code, as well as those running the databases (or other resources you may be using, such as MSMQ).

Here's how to enable DTC network access.

Upvotes: 1

Related Questions