0% found this document useful (0 votes)
36 views

Transaction Processing in Web Appllication

This code is performing a bank funds transfer transaction using ADO.NET and SQL Server. It opens a SQL connection and begins a transaction. It then updates two bank account amounts by subtracting from one account and adding to another within the transaction. If both updates succeed, it commits the transaction. Otherwise it rolls back the transaction. A button click handler is also provided to display the current bank account amounts in a grid view for verification.

Uploaded by

raghav
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Transaction Processing in Web Appllication

This code is performing a bank funds transfer transaction using ADO.NET and SQL Server. It opens a SQL connection and begins a transaction. It then updates two bank account amounts by subtracting from one account and adding to another within the transaction. If both updates succeed, it commits the transaction. Otherwise it rolls back the transaction. A button click handler is also provided to display the current bank account amounts in a grid view for verification.

Uploaded by

raghav
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System.Data.

SqlClient;
using System.Data;

namespace transaction
{
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
SqlConnection con;
SqlCommand cmd;
SqlTransaction trans;

protected void Button1_Click(object sender, EventArgs e)


{
con = new SqlConnection("Data Source=DESKTOP-F7EL1P6\\SQLEXPRESS;Initial
Catalog=master;Integrated Security=True;User ID=admin;Password=admin@123");
con.Open();
trans = con.BeginTransaction();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.Transaction = trans;
cmd.CommandText = "update bank set amount=amount-@amt where accno=@accno and
amount > @amt";
cmd.Parameters.Clear();
cmd.Parameters.Add("@accno",SqlDbType.Int).Value = TextBox1.Text;
cmd.Parameters.Add("@amt", SqlDbType.Decimal).Value = TextBox3.Text;
int flag1 = cmd.ExecuteNonQuery();
if (flag1 > 0)
{
cmd.CommandText = "update bank set amount=amount+@amt where accno=@accno
and amount > @amt";
cmd.Parameters["@accno"].Value = TextBox2.Text;
flag1 = cmd.ExecuteNonQuery();
if (flag1 > 0)
{
trans.Commit();
Response.Write("Transaction Success");

}
else
{
trans.Rollback();
Response.Write("Transaction failed");

}
else
{

Response.Write("Transaction failed");
}

protected void Button2_Click(object sender, EventArgs e)


{
con = new SqlConnection("Data Source=DESKTOP-F7EL1P6\\SQLEXPRESS;Initial
Catalog=master;Integrated Security=True;User ID=admin;Password=admin@123");
con.Open();
SqlDataAdapter sqa;
DataTable dt;

sqa = new SqlDataAdapter("select * from bank", con);


dt = new DataTable();
sqa.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

}
}
}

You might also like