How to implement transaction on website using ado.net

By
In previous tutorials  tutorial ,i have already  explained transactions in sql server.But now i am going to explain transactions  in real asp.net application.We can provide a save point to divide a transaction into sub logical transaction,Means if there are set of instruction in a transaction which must be executed and a set of statement will be executed or Rollback on dependent on some condition.Here we can provide a save point to those statement which must be committed. 
There are some steps to implement this concepts on website which are given below:-
Step 1:- First open your visual studio -->File-->New-->website-->select ASP.NET Empty website -->OK-->open  solution explorer -->Add a Web Form-->drag and drop Label ,Text Box and Button control from toolBox as shown below:-



transaction

Step 2:-Now open your sql server -->create a students table with three column(name,age,location).

Step 3:-Now Double click on insert button -->write the c# codes as given below:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string str = TextBox7.Text;
        SqlTransaction sqt = null;
        try
        {
            SqlConnection con = new SqlConnection("data source=Ramashanker-PC;Integrated Security=Yes;Database=master");
            con.Open();
            sqt = con.BeginTransaction();
            SqlCommand cmd = new SqlCommand("insert into students values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
            cmd.Transaction=sqt;
            cmd.ExecuteNonQuery();
            sqt.Save("my_trans");
            SqlCommand cmd2 = new SqlCommand("insert into students values('" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')", con);
            cmd2.Transaction = sqt;
            cmd2.ExecuteNonQuery();
            if(str!="yes")
            {
                sqt.Rollback("my_trans");
            }
            sqt.Commit();
            con.Close();
           }
            catch(Exception ex)
            {
              sqt.Rollback();
              Label1.Text=ex.Message;
            }
    }
}

Step 4:- Now Run the application(press F5)-->Enter the required field details and press Insert button as shown below:-

transaction_output

Step 5:- Now open your sql server -->you will see that both transactions data  have saved in students table.see it:-



Note:- When we Enter No instead of yes then only first transaction values are saved in students tables.

For More
  1. Connect database using oleDb
  2. How to implement Ajax based web services in asp.net
  3. How to use caching in asp.net application
  4. View state and hidden fields controls in asp.net
  5. How to send mail from asp.net web site 
  6. File and file info class in c#
  7. Generic collection in c#
  8. Method hiding in c#
Download attached file from below
         Download 

3 comments:

  1. System.NullReferenceException: Object reference not set to an instance of an object. for my_trans

    ReplyDelete
    Replies
    1. hi rajasekhar , check your each text box values correctly,means you are entered correct text box in coding part.

      Delete
    2. what my_trans did here... and where it was declared

      Delete

Powered by Blogger.