Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
If GlbSQLCOnn.State = ConnectionState.Closed Then
    GlbSQLCOnn.Open()
End If
'===========
SqlTrans = GlbSQLCOnn.BeginTransaction
Cmd.Transaction = SqlTrans
Cmd.CommandType = CommandType.Text
Cmd.CommandText = StrSQlDelete
Cmd.UpdatedRowSource = UpdateRowSource.None
Cmd.CommandTimeout = 10000

Cmd.ExecuteNonQuery()

Cmd.CommandText = StrSQlInsert & " OPTION (MAXDOP 2)"

Cmd.ExecuteNonQuery()


SqlTrans.Commit()
Posted
Updated 24-Apr-15 22:15pm
v2

1 solution

From what is to see of your code, the only explanation I can think of is that your SqlCommand Cmd isn't associated with the SqlConnection GlbSQLCOnn on which you started the transaction.

The name of your connection, GlbSQLCOnn, seems to indicate that it's a "global" connection object. There's no reason to do something like that. Connections to Sql-Server are pooled by default - so you gain nothing by re-using the same connection-object throughout the lifetime of your application. Instead it can become messy quickly (your problem here seems to be a case of that). That's why I suggest you to use any SqlClient-objects only locally.

Pseudo-code:
someMethod()
   using connection = new SqlConnection(connectionString)
   using transaction = connection.BeginTransaction
   using command = new SqlCommand(connection, transaction)

      connection.Open

      // do something here

      transaction.Commit

   end using
end method


Even if my assumption from the first paragraph isn't correct, you will solve your problem by sticking to the above "pattern".
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900