Error while retrieving @@Identity from SQL server

Please help, the program work fine without the "Private Sub sqlTransMtrDa_RowUpdated" procedure.
How when the procedure is existed, the following error will occurs!!!
The error message is :
"Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized."

Can you tell me why and how to solve this problem ?

Thanks in advance!

Imports System.Data.SqlClient
Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim SQLcnn As SqlConnection
    Dim SQLCnnStr As String
    Dim objTx As SqlTransaction
    Dim SqlDS As New DataSet
    Dim objCmdBuilder As SqlCommandBuilder
    Dim WithEvents sqlTransMtrDa As SqlDataAdapter

#Region " Windows Form Designer generated code "

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SQLCnnStr = "Persist Security Info=False" & _
           ";User ID=xxx" & _
           ";Password=xxx" & _
           ";Initial Catalog=xxx" & _
           ";Data Source=xxx" & _
           ";Connect Timeout=30"
        SQLCnn = New SqlClient.SqlConnection
        SQLCnn.ConnectionString = SQLCnnStr
        SQLcnn.Open()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim TxNo As String

        sqlTransMtrDa = New SqlDataAdapter("SELECT * FROM trans_mast", SQLcnn)

        objCmdBuilder = New SqlCommandBuilder(sqlTransMtrDa)
        sqlTransMtrDa.Fill(SqlDS, 1, 1, "trans_mast")

        sqlTransMtrDa.InsertCommand = objCmdBuilder.GetInsertCommand
        sqlTransMtrDa.UpdateCommand = objCmdBuilder.GetUpdateCommand
        sqlTransMtrDa.DeleteCommand = objCmdBuilder.GetDeleteCommand

        objTx = SQLcnn.BeginTransaction(IsolationLevel.Serializable)
        sqlTransMtrDa.InsertCommand.Transaction = objTx
        sqlTransMtrDa.UpdateCommand.Transaction = objTx
        sqlTransMtrDa.DeleteCommand.Transaction = objTx

        Try
            With SqlDS.Tables("trans_mast")
                Dim dr As DataRow = .NewRow

                dr.Item("comp_code") = "xxx"
                dr.Item("tx_type") = "INV"
                .Rows.Add(dr)
                sqlTransMtrDa.Update(SqlDS, "trans_mast")
            End With
            objTx.Commit()
            MsgBox("Updated")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub sqlTransMtrDa_RowUpdated(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlRowUpdatedEventArgs) Handles sqlTransMtrDa.RowUpdated
        If (e.StatementType = StatementType.Insert) Then
            Dim cmd As New SqlCommand("SELECT @@IDENTITY", SQLcnn)

            e.Row.Item("tx_no") = cmd.ExecuteScalar() ' <--------------- Error Occurs Here
            e.Row.AcceptChanges()
        End If

    End Sub
End Class
vbhahaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
eozz_2000Connect With a Mentor Commented:
Declare de objTx transaction in the properties of the class (out of any method).

And then in the RowUpdated event make this:

If e.StatementType = StatementType.Insert Then
  Dim cmd As New SqlCommand("SELECT @@IDENTITY", Me.conn, Me.objTx)
  MsgBox(cmd.ExecuteScalar)
End If
0
 
eozz_2000Commented:
The SQLConnection is working with the objTx transaction, and if the transaction has not made a commit or rollback the execution of the transaction still executing itself.

You must use another connection, or you can set the transaction of the cmd command in your sqlTransMtrDa_RowUpdated event to objTx
0
 
eozz_2000Commented:
I hope this could help you :)
0
 
vbhahaAuthor Commented:
eozz_2000, although it can work after changing the connection, I cannot get the "Identity" number, in fact it returned "null" !
0
 
vbhahaAuthor Commented:
eozz_2000

It works!

In addition, I have changed the dim statement into :

Dim cmd As New SqlCommand("SELECT @@IDENTITY", e.Command.Connection, e.Command.Transaction)

So that I don't have to worry about the connection and transaction object.

Anyway thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.