How to check if Transaction is Commited or Rolledback?

vishalneeraj-24503
Posted by vishalneeraj-24503 under ADO.NET category on | Points: 40 | Views : 4252
We can check if Transaction is Commited or Rolledback by doing simple operations.

Make a function named check_if_transaction_rolledback_committed and take two reference variables as shown in below code:-

private void check_if_transaction_rolledback_committed(ref bool is_transaction_commited, ref bool is_transaction_rolledback)
{
using (SqlConnection con = new SqlConnection())
{
using (SqlTransaction tran = con.BeginTransaction())
{
try
{
//DML operations goes here

//Commit all changes
tran.Commit();
is_transaction_commited = true;
}
catch (Exception ex)
{
//Rollback all changes
tran.Rollback();
is_transaction_rolledback = true;
throw ex;
}
}
}
}


In btn_save event check:-
protected void btn_save_Click(object sender, EventArgs e)
{
bool is_transaction_commited = false;
bool is_transaction_rolledback = false;

check_if_transaction_rolledback_committed(ref is_transaction_commited, ref is_transaction_rolledback);

if (is_transaction_commited)
{
lbl_message.Text = "Transaction is commited";
}

if (is_transaction_rolledback)
{
lbl_message.Text = string.Empty;

lbl_message.Text = "Transaction is rolledback";
}
}

Comments or Responses

Login to post response