Does anybody understand how to apply transaction roll in database object insinternet c#?

Example: I've the next code initially:

   protected void btnSave_Click(object sender, EventArgs e)
   {
      try {
         m_test = CreateTest();
      } catch{}
   }


   private Db.Test CreateTest()
   {
           var test= new Test();
           test.Title = sTitle.SelectedValue;
           test.FirstName = sFirstName.Text;
           test.LastName = sLastName.Text;
           test.PhoneHome = sHomePhone.Text;
           test.PhoneWork = sWorkPhone.Text;
           test.PhoneMobile = sMobile.Text;
           test.EmailAddress = sEmail.Text;
           test.Save();

           return test;
   }

I Quickly attempting to apply transaction roll back action in to the code and applied the next code inside... i quickly stack underneath the method CreateTest. I acquired no clue how could I applied the code for that database object

       protected void btnSave_Click(object sender, EventArgs e)
       {

          SqlConnection connDB = new SqlConnection();
          SqlCommand cmdExecuting = new SqlCommand();

          try {
             connDB = new SqlConnection(connection_string);
             cmdExecuting.Connection = connDB;
             connDB.Open();
             cmdExecuting.Transaction = connDB.BeginTransaction();

             m_test = CreateTest(cmdExecuting);

            if (m_test != 0) {
                 cmdExecuting.Transaction.Rollback();
                 return;
            }
          } catch{}
       }


   private Db.Test CreateTest(SqlCommand  cmdExecuting)
   {
           var test= new Test();
           test.Title = sTitle.SelectedValue;
           test.FirstName = sFirstName.Text;
           test.LastName = sLastName.Text;
           test.PhoneHome = sHomePhone.Text;
           test.PhoneWork = sWorkPhone.Text;
           test.PhoneMobile = sMobile.Text;
           test.EmailAddress = sEmail.Text;
           test.Save();

           return test;
   }

Does anybody know how do i code within the CreateTest() within this situation?

Have a look in the TransactionScope class, which supplies several advantages including

  • Automatic transaction enlistment of connections made within scope
  • Automatic rollback of transactions when exceptions occur
  • For me additionally, it helps make the code a little more legible because it reduces the quantity of code needed

.

try    
{   
    using (TransactionScope scope = new TransactionScope())     
    {                                   
        m_test = CreateTest();          
        scope.Complete(); // Commit transaction     
    }    
}    
catch (Exception ex)    
{
    // Transaction is automatically rolled back
}

Also, begin to see the article Introducing System.Transactions in the .NET Framework 2.0 for any great beginning point.

you are able to take assistance of the next its not necessary to consider proper care of transaction at after sales sqltransaction object will require proper care of it. place the this code in your event whichever accounts for placing the information within the database

 protected void Button1_Click(object sender, EventArgs e)
    {
      SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;uid=sa;pwd=sa;");
      myConnection.Open();

      // Start a local transaction
      SqlTransaction myTrans = myConnection.BeginTransaction();

      SqlCommand myCommand = new SqlCommand();
      myCommand.Connection = myConnection;
      myCommand.Transaction = myTrans;
      try
      {
        myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
        myCommand.ExecuteNonQuery();
        myCommand.CommandText = "delete * from Region where RegionID=101";

        // Attempt to commit the transaction. 
        myCommand.ExecuteNonQuery();
        myTrans.Commit();
        Response.Write("Both records are written to database.");
      }
      catch (Exception ep)
      {
        // Attempt to roll back the transaction. 
        myTrans.Rollback();
        Response.Write(ep.ToString());
        Response.Write("Neither record was written to database.");
      }
      finally
      {
        myConnection.Close();
      }
    }