/// <summary> /// Handles the Click event of the btnSaveAll control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void btnSaveAll_Click(object sender, EventArgs e) { // use transaction to commit toDetele and toUpdate items at once Transaction transactionManager = new Transaction(IsolationLevel.ReadCommitted, "ProductsTrans"); try { // save products transactionManager.Add(_productsCollection); _productsCollection.SaveMulti(); // delete products transactionManager.Add(_productsToDelete); _productsToDelete.DeleteMulti(); // save the whole transaction transactionManager.Commit(); // no errors... clean the toDetele collection _productsToDelete.Clear(); // successfully message MessageBox.Show("Data was saved successfully!", "Products", MessageBoxButtons.OK, MessageBoxIcon.Information); } // there are errors. Rollback the transaction and show the errors to the user. catch (Exception ex) { transactionManager.Rollback(); MessageBox.Show(ex.Message, "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void btnSave_Click(object sender, EventArgs e) { // persist changes to DB _productsCollection.SaveMulti(); // persist user deletion to DB _productsToDelete.DeleteMulti(); // show message to user MessageBox.Show("Changes saved."); }