Solved

Error while retrieving @@Identity from SQL server

Posted on 2004-10-06
5
266 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:vbhaha
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12244977
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12244982
I hope this could help you :)
0
 

Author Comment

by:vbhaha
ID: 12245031
eozz_2000, although it can work after changing the connection, I cannot get the "Identity" number, in fact it returned "null" !
0
 
LVL 4

Accepted Solution

by:
eozz_2000 earned 500 total points
ID: 12245118
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
 

Author Comment

by:vbhaha
ID: 12245225
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now