Introducing LINQ to SQL Designer using Visual Studio 2008 - Creating a simple LINQ to SQL application: handling transactions using LINQ
(Page 4 of 5 )
Now that you are familiar with fetching and updating database tables using LINQ (in previous sections), it is time to work with transactions.
"LINQ to SQL" has built-in support for transactions. If we use "LINQ to SQL Designer," we don't even have to worry about transactions. If multiple rows are modified (irrespective of any number of tables), "LINQ to SQL" automatically encloses them in a transaction.
Let us start with another web page. Follow the steps below:
Add a new web page (.aspx) to your project. Call it "TransactionSample.aspx."
Design your web page as shown below (Fig 10):

Make modifications to the source to make it look like the following:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TransactionSample.aspx.vb" Inherits="SampleVB.TransactionSample" %>
<!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>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td valign="top">
<asp:Button ID="btnRefreshDept" runat="server" Text="Refresh Dept" />
<asp:GridView ID="gvDept" runat="server">
</asp:GridView>
</td>
<td valign="top">
<asp:Button ID="btnRefreshEmp" runat="server" Text="Refresh Emp" />
<asp:GridView ID="gvEmp" runat="server">
</asp:GridView>
</td>
</tr>
</table>
<asp:Button ID="btnAddDeptEmp" runat="server" Text="Do Transaction" /><br />
<asp:Label ID="lblMsg" runat="server" ForeColor="Maroon"></asp:Label>
</div>
</form>
</body>
</html>
Imports System.Data.Linq
Partial Public Class TransactionSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.lblMsg.Text = ""
End Sub
Protected Sub btnRefreshDept_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRefreshDept.Click
Dim db As New SampleDBDataContext
Me.gvDept.DataSource = db.depts
Me.gvDept.DataBind()
End Sub
Protected Sub btnRefreshEmp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRefreshEmp.Click
Dim db As New SampleDBDataContext
Me.gvEmp.DataSource = db.emps
Me.gvEmp.DataBind()
End Sub
Protected Sub btnAddDeptEmp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddDeptEmp.Click
Try
Dim db As New SampleDBDataContext
Dim depts As Table(Of dept) = db.depts
'create new dept
Dim NewDept As New dept
NewDept.deptno = 50
NewDept.dname = "Legal"
'create new emp
Dim NewEmp1 As New emp
NewEmp1.empno = 2001
NewEmp1.ename = "aaa"
NewEmp1.sal = "2500"
NewEmp1.deptno = 50
'create another emp
Dim NewEmp2 As New emp
NewEmp2.empno = 2002
NewEmp2.ename = "bbb"
NewEmp2.sal = "5500"
NewEmp2.deptno = 50
'add employees to dept
NewDept.emps.Add(NewEmp1)
NewDept.emps.Add(NewEmp2)
'add to context
db.depts.InsertOnSubmit(NewDept)
'save the changes
db.SubmitChanges()
Me.lblMsg.Text = "Saved successfully"
Catch ex As Exception
Me.lblMsg.Text = ex.Message
End Try
End Sub
End Class
Once executed, the output should look as follows (Fig 11):

The next section gives you an understanding of the transactions.
Next: Explanation of handling transactions in LINQ to SQL >>
More .NET Articles
More By Jagadish Chaterjee