Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I Have the following Code.

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sqlcmd As New SqlClient.SqlCommand

    Dim con As New SqlClient.SqlConnection("Data Source=kgmmurugesh\sqlexpress;Initial Catalog=KGMERP_STORES;Integrated Security=True")

    Try
        con.Open()
        sqlcmd.Connection = con
        sqlcmd.CommandText = "begin tran ItemCreation"
        sqlcmd.ExecuteNonQuery()

        sqlcmd.CommandText = "Insert into Item_table(Item_Name,Item_PRate) values ('apple','a150')"
        sqlcmd.ExecuteNonQuery()

        sqlcmd.CommandText = "commit tran ItemCreation"
        sqlcmd.ExecuteNonQuery()

    Catch ex As Exception

        sqlcmd.CommandText = "rollback tran ItemCreation"
        sqlcmd.ExecuteNonQuery()

    End Try

End Sub



It works well, but sometimes it shows the following error:
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.

what is the mistake in my code?
Posted

The rollback doesn't seem to be part of the original transaction, it's on a separate sqlcmd
 
Share this answer
 
Comments
kgmmurugesh 25-Jun-14 9:47am    
I use only one sqlcmd in my code.
You need to perform SqlTransaction.Commit() when success.
and SqlTransaction.Rollback() in Catch.

Refer this links : Commit and
Rollback
 
Share this answer
 
Comments
kgmmurugesh 25-Jun-14 9:49am    
I use commit tran in sucess and rollback when error in my code.
Sprint89 25-Jun-14 9:57am    
But that is in your SQL code. If you want to do it that way you need to do the whole transaction, commit/rollback/error-handling in the one SqlCommand. The other way to do it (as in solution2) is to handle the transaction in VB
kgmmurugesh 25-Jun-14 10:02am    
I don't want to do this in sqltransaction, tell me the error in my code!
You would need something along the lines of:


VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sqlcmd As New SqlClient.SqlCommand
 
    Dim con As New SqlClient.SqlConnection("Data Source=kgmmurugesh\sqlexpress;Initial Catalog=KGMERP_STORES;Integrated Security=True")
 
        con.Open()
        sqlcmd.Connection = con
        sqlcmd.CommandText = @"begin tran  ItemCreation;
		                   insert into Item_table(Item_Name,Item_PRate) values ('apple','a150')
                               if @@error <> 0
                                   rollback tran ItemCreation;
			       else
				   commit tran ItemCreation; "       
	
	sqlcmd.ExecuteNonQuery()

End Sub


The SQL is OK but I don't have Visual Studio open right now so I'm not sure about the VB. Thing is, your VB code won't know if your transaction has committed or rolled back
 
Share this answer
 
v2
Comments
kgmmurugesh 26-Jun-14 2:03am    
I use the same logic as you said, if there is no error then i commit the transaction otherwise roll back the transaction. It shows error in some systems only, how?
I'm not going to explain this particularly well, but your problem is that there is nothing to "tie together" all those ExecuteNonQuery statements. Each of them "stands alone" - similar to putting GO into a SQL query directly - for example
SQL
DECLARE @T VARCHAR(20);
GO
SET @T='Hello World'
will throw an error because @T is not declared by the time you execute the SET. This is the basis of Solution 3 - putting all of the sql into a single ExecuteNonQuery statement is one way around your problem. Putting your sql (including the rollback) into a Stored Procedure is another alternative.

This very good CodeProject article - Using Transactions in ADO.NET[^] - explains the concepts far better than I and includes an example solution that will fit right in with what you already have
 
Share this answer
 
Comments
kgmmurugesh 26-Jun-14 1:58am    
as you said sqlquries are stand alone, but I issued Begin Transaction fist then how it makes error?

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