Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have one prob with this 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=office\kgmEXPRESS;Initial Catalog=KGMERP_STORES;Integrated Security=True")

    Try
        con.Open()
        sqlcmd = New SqlClient.SqlCommand("begin tran ItemCreation", con)
        sqlcmd.ExecuteNonQuery()

        Dim cmd As New SqlClient.SqlCommand("Insert into Item_table(Item_Name,Item_PRate) values ('apple',150)", con)
        cmd.ExecuteNonQuery()

        sqlcmd = New SqlClient.SqlCommand("commit tran ItemCreation", con)
        sqlcmd.ExecuteNonQuery()

    Catch ex As Exception

        sqlcmd = New SqlClient.SqlCommand("rollback tran ItemCreation", con)
        sqlcmd.ExecuteNonQuery()

    End Try

End Sub

when the bold line code executes, if any error comes i want to rollback with the code in exception. But i met with error : The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.

What is the solution for this?

Regards
Posted
Updated 22-Jun-14 19:27pm
v2

1 solution

try this.. :)

VB
   Dim transaction As SqlTransaction
   con.Open()
   transaction = con.BeginTransaction()

Try

           Dim cmd As New SqlClient.SqlCommand("Insert into Item_table(Item_Name,Item_PRate) values ('apple',150)", con)
           cmd.ExecuteNonQuery()
           transaction.Commit()
       Catch ex As Exception

          transaction.Rollback()

       End Try
 
Share this answer
 
v3
Comments
kgmmurugesh 24-Jun-14 1:16am    
It works well, I want to know what is the mistake in my code?

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