Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to apply transaction roll back in database object in .net c#
    text
    copied!<p>Does anyone know how to apply transaction roll back in database object in .net c#?</p> <p>Example: I have the following code originally:</p> <pre><code> 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; } </code></pre> <p>Then I trying to apply transaction roll back action into the code and applied the following code inside... then I stack under the method CreateTest. I got no idea how could I applied the code for the database object</p> <pre><code> 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; } </code></pre> <p>Does anyone know how can I code in the CreateTest() in this case?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload