.NET
  Home arrow .NET arrow Page 4 - Introducing LINQ to SQL Designer using Vis...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Introducing LINQ to SQL Designer using Visual Studio 2008
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2008-04-07

    Table of Contents:
  • Introducing LINQ to SQL Designer using Visual Studio 2008
  • Creating a simple LINQ to SQL application: fetching information onto a web page
  • Creating a simple LINQ to SQL application: updating information to the database
  • Creating a simple LINQ to SQL application: handling transactions using LINQ
  • Explanation of handling transactions in LINQ to SQL

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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>


    • Turn to the code behind the page (TransactionSample.aspx.vb) and modify the code to look like the following:


    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.

    More .NET Articles
    More By Jagadish Chaterjee


       · Hello guys, This is my second article in the series and is an introductory...
     

    .NET ARTICLES

    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway