.NET
  Home arrow .NET arrow Page 3 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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: 5 stars5 stars5 stars5 stars5 stars / 19
    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


    Introducing LINQ to SQL Designer using Visual Studio 2008 - Creating a simple LINQ to SQL application: updating information to the database


    (Page 3 of 5 )

    Let us start adding a new web page to the project.

    • Add a new web page (.aspx) to your project

    • Design your web page to look like the following (Fig 08):

    • Modify your code behind to look like the following:


    Imports System.Data.Linq

    Partial Public Class CRUDSample

    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 btnSelectStar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSelectStar.Click

    Dim db As New SampleDBDataContext

    Me.GridView1.DataSource = db.emps

    Me.GridView1.DataBind()

    End Sub


    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

    Try

    Dim db As New SampleDBDataContext

    'getting an existing row

    Dim objEmp As emp = db.emps.SingleOrDefault(Function(p) p.empno = Me.txtEmpno.Text)

    If objEmp IsNot Nothing Then

    Me.txtEname.Text = objEmp.ename

    Me.txtSal.Text = objEmp.sal

    Me.txtDeptno.Text = objEmp.deptno

    Else

    Me.lblMsg.Text = "Employee not found"

    End If

    Catch ex As Exception

    Me.lblMsg.Text = ex.Message

    End Try

    End Sub


    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click

    Try

    Dim db As New SampleDBDataContext

    'adding a new row

    Dim rEmp As emp = New emp With {.empno = Me.txtEmpno.Text, .ename = Me.txtEname.Text, .sal = Me.txtSal.Text, .deptno = Me.txtDeptno.Text}

    db.emps.InsertOnSubmit(rEmp)

    'saving rows added

    db.SubmitChanges()

    Me.lblMsg.Text = "Added Successfully"

    Catch ex As Exception

    Me.lblMsg.Text = ex.Message

    End Try

    End Sub


    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click

    Try

    Dim db As New SampleDBDataContext

    'getting an existing row

    Dim objEmp As emp = db.Emps.SingleOrDefault(Function(p) p.empno = Me.txtEmpno.Text)

    If objEmp IsNot Nothing Then

    'modifying the row

    objEmp.ename = Me.txtEname.Text

    objEmp.sal = Me.txtSal.Text

    objEmp.deptno = Me.txtDeptno.Text

    'saving rows added

    db.SubmitChanges()

    Me.lblMsg.Text = "Updated Successfully"

    Else

    Me.lblMsg.Text = "Employee not found"

    End If

    Catch ex As Exception

    Me.lblMsg.Text = ex.Message

    End Try

    End Sub


    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click

    Try

    Dim db As New SampleDBDataContext

    'getting an exiting row

    Dim objEmp As emp = db.emps.SingleOrDefault(Function(p) p.empno = Me.txtEmpno.Text)

    If objEmp IsNot Nothing Then

    'delete the row

    db.emps.DeleteOnSubmit(objEmp)

    'saving rows deleted

    db.SubmitChanges()

    Me.lblMsg.Text = "Deleted Successfully"

    Else

    Me.lblMsg.Text = "Employee not found"

    End If

    Catch ex As Exception

    Me.lblMsg.Text = ex.Message

    End Try

    End Sub

    End Class

    Now you can observe that the above code is bit different from my previous article.

    How about dealing with transactions? Does "LINQ to SQL" support transactions? The next section gives you the answer.

    More .NET Articles
    More By Jagadish Chaterjee


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

    .NET ARTICLES

    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT