Create transaction using the DataAccess objects in ASP.NET


Presentation Layer:

<%@ Page Language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Transaction.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>

 <head>

    <meta name="vs_showGrid" content="False">

    <title>WebForm1</title>

 </head>

 <body bgcolor="#ffffcc">

    <h2>

        <font style="color: red; font-style: italic; font-variant: small-caps">Working of a

            Connection Transaction Object</font>

    </h2>

    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">

    <meta name="CODE_LANGUAGE" content="C#">

    <meta name="vs_defaultClientScript" content="JavaScript">

    <meta name="vs_targetSchema"

      content="http://schemas.microsoft.com/intellisense/ie5">

    <form id="Form1" method="post" runat="server">

        <asp:Button ID="Button1" Style="z-index: 101; left: 16px; position: absolute; top:

          344px" runat="server" BorderStyle="Solid" Width="112px"

          Text="BeginTransaction"></asp:Button>

        <asp:Button ID="Button2" Style="z-index: 102; left: 128px; position: absolute;

          top: 344px" runat="server" Text="AddRow" BorderStyle="Solid"></asp:Button>

        <asp:Button ID="Button3" Style="z-index: 103; left: 200px; position: absolute;

          top: 344px" runat="server" Text="DeleteRow" Width="72px"

          BorderStyle="Solid"></asp:Button>

        <asp:Button ID="Button4" Style="z-index: 104; left: 272px; position: absolute;

          top: 344px" runat="server" Text="CommitTrans" Width="88px"

          BorderStyle="Solid"></asp:Button>

        <asp:Button ID="Button5" Style="z-index: 105; left: 360px; position: absolute;

          top: 344px" runat="server" Text="RollBack" BorderStyle="Solid"></asp:Button>

        <asp:DataGrid ID="DataGrid1" Style="z-index: 106; left: 64px; position: absolute;

            top: 56px" runat="server" Width="257px">

        </asp:DataGrid>

        <asp:TextBox ID="TextBox1" Style="z-index: 107; left: 464px; position: absolute;

            top: 200px" runat="server" Height="24px"></asp:TextBox>

        <asp:Label ID="Label1" Style="z-index: 108; left: 472px; position: absolute; top:

          160px" runat="server" Width="112px" Height="6px">Enter EmpID</asp:Label>

    </form>

    </FONT>

    <h2>

    </h2>

    <h2>

    </h2>

 </body>
</html>

Application Logic:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

 

namespace Transaction

{

    /// <summary>

    /// Summary description for WebForm1.

    /// </summary>

    public class WebForm1 : System.Web.UI.Page

    {

        protected System.Web.UI.WebControls.Button Button1;

        protected System.Web.UI.WebControls.Button Button2;

        protected System.Web.UI.WebControls.Button Button3;

        protected System.Web.UI.WebControls.Button Button4;

        protected System.Web.UI.WebControls.Button Button5;

        public OleDbConnection con;

        public static DataSet ds;

        protected System.Web.UI.WebControls.DataGrid DataGrid1;

        public static OleDbDataAdapter odap;

        public OleDbCommandBuilder cmb;

        protected System.Web.UI.WebControls.TextBox TextBox1;

        protected System.Web.UI.WebControls.Label Label1;

        protected System.Web.UI.HtmlControls.HtmlForm Form1;

        public static OleDbTransaction otrans;

 

        private void Page_Load(object sender, System.EventArgs e)

        {

            // Put user code to initialize the page here

        }

 

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)

        {

            //

            // CODEGEN: This call is required by the ASP.NET Web Form Designer.

            //

            InitializeComponent();

            base.OnInit(e);

        }

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.Button1.Click += new System.EventHandler(this.Button1_Click);

            this.Button2.Click += new System.EventHandler(this.Button2_Click);

            this.Button3.Click += new System.EventHandler(this.Button3_Click);

            this.Button4.Click += new System.EventHandler(this.Button4_Click);

            this.Button5.Click += new System.EventHandler(this.Button5_Click);

            this.Load += new System.EventHandler(this.Page_Load);

        }

        #endregion

 

        private void Button1_Click(object sender, System.EventArgs e)

        {

            con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data

            Source=d:\\Employee.mdb");

            odap = new OleDbDataAdapter("select * from emp", con);

            ds = new DataSet();

            con.Open();

            //Transaction Object Nullifies the Update Command of DataAdapter Object

            otrans = con.BeginTransaction();

            odap.SelectCommand.Transaction = otrans;

            odap.Fill(ds, "emp");

            DataGrid1.DataSource = ds;

            DataGrid1.DataBind();

        }

 

        private void Button2_Click(object sender, System.EventArgs e)

        {

            //Adding a Row into the DataSet and updating the Resultant

            //into the DataBase using the Adapter Object         

 

            DataTable dt = ds.Tables[0];

            DataRow dr = dt.NewRow();

            cmb = new OleDbCommandBuilder(odap);

            dr[0] = "xc2";

            dr[1] = "Arjun";

            dr[2] = 23000;

            dt.Rows.Add(dr);

            odap.Update(ds, "emp");

            DataGrid1.DataSource = ds;

            DataGrid1.DataBind();

        }

 

        private void Button3_Click(object sender, System.EventArgs e)

        {

            //Deleting the Rows  from DataSet and Updating the Resultant DataSet

            // into the DataBase using Adapter Object

            DataTable dt = ds.Tables[0];

            DataRow[] dr = dt.Select("empid ='" + TextBox1.Text.Trim() + "'");

            foreach (DataRow dre in dr)

                dre.Delete();

            odap.Update(ds, "emp");

            DataGrid1.DataSource = ds;

            DataGrid1.DataBind();

        }

 

        private void Button4_Click(object sender, System.EventArgs e)

        {

            otrans.Commit();

        }

 

        private void Button5_Click(object sender, System.EventArgs e)

        {

            otrans.Rollback();

        }

    }

}


Similar Articles