Presentation Layer:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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> Working of a Connection Transaction Object </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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: 318px; position: absolute;
top: 231px" runat="server" Height="24px"></asp:TextBox>
<asp:Label ID="Label1" Style="z-index: 108; left: 178px; position: absolute; top:
235px" runat="server" Width="112px" Height="6px">Enter EmpID</asp:Label>
</div>
</form>
</body>
</html>
Application Logic:
Imports System
Imports System.Collections
Imports System.Data
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
Public con As OleDbConnection
Public Shared ds As DataSet
Public Shared odap As OleDbDataAdapter
Public cmb As OleDbCommandBuilder
Public Shared otrans As OleDbTransaction
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
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()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'Adding a Row into the DataSet and updating the Resultant
'into the DataBase using the Adapter Object
Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow = 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()
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
'Deleting the Rows from DataSet and Updating the Resultant DataSet
' into the DataBase using Adapter Object
Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow() = dt.[Select]("empid ='" + TextBox1.Text.Trim() + "'")
For Each dre As DataRow In dr
dre.Delete()
Next
odap.Update(ds, "emp")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
otrans.Commit()
End Sub
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
otrans.Rollback()
End Sub
End Class
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).