C# Ado.net Transaction example:
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Transaction.aspx.cs" Inherits="Transaction" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Transaction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = null; new SqlConnection(WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "update SampleAccount set Balance = Balance - @Amount where Id='A'";
cmd1.Connection = con;
cmd1.Parameters.Add(new SqlParameter("@Amount", Convert.ToInt32(TextBox1.Text)));
SqlCommand cmd2 = new SqlCommand("update SampleAccount set Balance = Balance + @Amount where Id='B'", con);
cmd2.Parameters.Add(new SqlParameter("@Amount", Convert.ToInt32(TextBox1.Text)));
SqlTransaction trans = null;
try
{
con.Open();
trans = con.BeginTransaction();
cmd1.Transaction = trans;
cmd1.ExecuteNonQuery();
trans.Save("pankaj");
throw new ApplicationException();
cmd2.Transaction = trans;
cmd2.ExecuteNonQuery();
trans.Commit();
Page.Response.Write("Success");
}
catch (Exception)
{
trans.Rollback("pankaj");
Page.Response.Write("Fail");
trans.Commit();
}
finally
{
con.Close();
}
}
}
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Transaction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = null; new SqlConnection(WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "update SampleAccount set Balance = Balance - @Amount where Id='A'";
cmd1.Connection = con;
cmd1.Parameters.Add(new SqlParameter("@Amount", Convert.ToInt32(TextBox1.Text)));
SqlCommand cmd2 = new SqlCommand("update SampleAccount set Balance = Balance + @Amount where Id='B'", con);
cmd2.Parameters.Add(new SqlParameter("@Amount", Convert.ToInt32(TextBox1.Text)));
SqlTransaction trans = null;
try
{
con.Open();
trans = con.BeginTransaction();
cmd1.Transaction = trans;
cmd1.ExecuteNonQuery();
trans.Save("pankaj");
throw new ApplicationException();
cmd2.Transaction = trans;
cmd2.ExecuteNonQuery();
trans.Commit();
Page.Response.Write("Success");
}
catch (Exception)
{
trans.Rollback("pankaj");
Page.Response.Write("Fail");
trans.Commit();
}
finally
{
con.Close();
}
}
}