calling ExecuteActionQuery directly

Posts   
 
    
GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 17-May-2006 22:46:17   

I'm trying to add the function below to DataAccessAdapter, and then call from OnSaveEntityComplete of DataAccessAdapter. Not sure if i can do this. I would like this to be part of the same transaction. I am calling from OnSaveEntityComplete so i can get autogenerated Ids.


Public Sub InsertAuditRecord(ByVal EntityName As String, ByVal EntityID As String, ByVal EventName As String, ByVal EventDetails As String)

            Dim cmd As New System.Data.SqlClient.SqlCommand("Insert Into tblAudit(EntityName, EntityID, EventName, EventDetails, UserName) VALUES(@EntityName, @EntityID, @EventName, @EventDetails, @UserName)")

            cmd.Transaction = CType(Me.PhysicalTransaction, SqlTransaction)
            cmd.Parameters.Add(New SqlParameter("@EntityName", EntityName))
            cmd.Parameters.Add(New SqlParameter("@EntityID", EntityID))
            cmd.Parameters.Add(New SqlParameter("@EventName", EventName))
            cmd.Parameters.Add(New SqlParameter("@EventDetails", EventDetails))
            cmd.Parameters.Add(New SqlParameter("@UserName", LM.Data.DataApplication.Globals.CurrentUserName))

            Dim actionQuery As New actionQuery(cmd)
            Me.ExecuteActionQuery(actionQuery)

End Sub
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-May-2006 07:50:34   

Instead of using a sqlCommand object and Action Query why don't you use the same Adapter instance "Me" to execute the query, which will run in the same transaction, if you have already opened a transaction.

Anyway, please check the following thread which might be helpful: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=4883

GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 18-May-2006 18:03:32   

my Audit table is not an LLBLGen Entity. I did find a way to do this if there is a transaction open.

I can get (me/this) dataaccessadapter.PhysicalTransaction and dataaccessadapter.PhysicalTransaction.Connection

like this


        Public Sub InsertAuditRecord(ByVal EntityName As String, ByVal EntityID As String, ByVal EventName As String, ByVal EventDetails As String)

            Dim transaction As SqlTransaction
            Dim connection As SqlConnection

            Dim isConnectionCreatedByMe As Boolean = False

            If Me.PhysicalTransaction Is Nothing Then
                isConnectionCreatedByMe = True
                connection = New SqlConnection(Me.ConnectionString)
                connection.Open()
                transaction = Nothing
            Else
                isConnectionCreatedByMe = False
                connection = CType(Me.PhysicalTransaction.Connection, SqlConnection)
                transaction = CType(Me.PhysicalTransaction, SqlTransaction)
            End If


            Dim cmd As New System.Data.SqlClient.SqlCommand("Insert Into tblAudit(EntityName, EntityID, EventName, EventDetails, UserName) VALUES(@EntityName, @EntityID, @EventName, @EventDetails, @UserName)", connection, transaction)
            Try
                cmd.Transaction = CType(Me.PhysicalTransaction, SqlTransaction)
                cmd.Parameters.Add(New SqlParameter("@EntityName", EntityName))
                cmd.Parameters.Add(New SqlParameter("@EntityID", EntityID))
                cmd.Parameters.Add(New SqlParameter("@EventName", EventName))
                cmd.Parameters.Add(New SqlParameter("@EventDetails", EventDetails))
                cmd.Parameters.Add(New SqlParameter("@UserName", Labelmaster.Data.DataApplication.Globals.CurrentUserName))
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                If Not transaction Is Nothing Then
                    cmd.Transaction.Rollback()
                End If
                Throw New ApplicationException("Could not create audit message for " & EntityName & ". See inner exception for more details", ex)
            Finally
                If isConnectionCreatedByMe Then
                    connection.Close()
                End If
            End Try
        End Sub


it appears that OnSaveEntityComplete is within a transaction, but OnDeleteEntityComplete isn't. I'm sure this could be something i am doing when calling save or delete.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 19-May-2006 09:27:20   

Perhaps I'm overlooking something, but I can't seem to find the lines in your code where you do the Save and Delete, so I don't see where you would trigger OnSaveEntityComplete and OnDeleteEntityComplete...

Frans Bouma | Lead developer LLBLGen Pro
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 19-May-2006 09:29:46   

Doing a Save to an entity with a recurse = true parameter, to save any related entities, starts a transaction under the hood.

While deletes are done only to single entities (no need for a transaction), you have to do the recursion your self. But in your case, you may explicitly start a transaction before calling the delete method.