Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
           
            con.Open();
            SqlCommand cmd1 = new SqlCommand();
            SqlCommand cmd2 = new SqlCommand();
            SqlTransaction trn=con.BeginTransaction();
            cmd1.Transaction = trn;
            cmd2.Transaction = trn;
            try
            {
            cmd1.CommandText = "INSERT INTO Table_1 VALUES(64,1)"; //This statment //does not violate the primary key constraint
            cmd1.Connection = con;
            cmd1.ExecuteNonQuery();
            trn.Save("FirstTran"); //Save Point
            cmd2.CommandText = "INSERT INTO Table_1 VALUES(60,1)";//This statment 
// violates the primary key constraint
            cmd2.Connection = con;
            cmd2.ExecuteNonQuery();
             MessageBox.Show("Name of the Connection: "+trn.Connection.ToString());
             MessageBox.Show("Isolation Level of the Transaction: " + trn.IsolationLevel);
             trn.Commit();
            }
            catch (Exception)
            {
                trn.Rollback("FirstTran");
            }
            finally
            {
                con.Close();
            }

        }


I wrote this C# code but the savepoint concept here is not working. The first statement is a valid INSERT statement and second INSERT statement violates the primary key. Hence the cmd2.ExecuteNonQuery() gives an error and control goes inside the CATCH block. According to my knowledge only statment 2 should have been rolled back. But both statments are getting rolled back.
Please Help!!!
Posted

1 solution

look at the code:

C#
 SqlTransaction trn=con.BeginTransaction(); //It's begining the transaction block

//Transaction Block:

//...

cmd2.ExecuteNonQuery(); // the sequence of the program is breaking here and it's jumping on chatch (.... because of the exception and never be commited.

//...             

trn.Commit();
}
catch (Exception)
{
     trn.Rollback("FirstTran");

//Transaction block end.

}
finally
{
     con.Close();
}



//

Transaction makes an atomic block and all of the code in the Block will be Commited or will be rolled back. That's why both of statments are rolled back.


About the primary key error:
SQL
INSERT INTO Table_1 (Column1, Column2)
                     VALUES(60,1)

If Column2 is not a PK or UK and if you have an ID that is identity(1,1) then you don't have any problem with Primary Key Error.

Check this link please:

http://support.microsoft.com/kb/238163[^]
 
Share this answer
 
v5
Comments
puja11191 4-Mar-12 7:29am    
But what is the role of
trn.Save("FirstTran");
Why this is not working?
What is the correct way of working with save points.
Please reply!
Shahin Khorshidnia 4-Mar-12 8:53am    
I think it can help you:

http://support.microsoft.com/kb/238163
shivani 2013 6-Feb-13 6:43am    
if i want to affect more than one table........hw is it possible????????

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