.NET
  Home arrow .NET arrow Page 5 - Beginning LINQ to SQL Using Visual Studio ...
Moblin
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 
Iron Speed
 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

Beginning LINQ to SQL Using Visual Studio 2008
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2008-03-31

    Table of Contents:
  • Beginning LINQ to SQL Using Visual Studio 2008
  • Developing a simple LINQ to SQL application (without LINQ designer): UI Design and source in VB.NET
  • Developing a simple LINQ to SQL application (without LINQ designer): source in C#
  • CRUD operations using the LINQ to SQL application (without LINQ designer): UI Design
  • CRUD operations using the LINQ to SQL application (without LINQ designer): Source in VB.NET
  • CRUD operations using the LINQ to SQL application (without LINQ designer): Source in C#

  • 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!

    Beginning LINQ to SQL Using Visual Studio 2008 - CRUD operations using the LINQ to SQL application (without LINQ designer): Source in VB.NET


    (Page 5 of 6 )

    Once the UI is designed as discussed in the previous section, we need to code for the button clicks (in code-behind) for all add, update, delete, search and refresh buttons.

    The following is the source code in VB.NET 2008:


    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 DataContext(New System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"))

    Dim tblEmps As Table(Of Emp) = db.GetTable(Of Emp)()

    Me.GridView1.DataSource = tblEmps

    Me.GridView1.DataBind()

    End Sub


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

    Try

    'getting table

    Dim db As New DataContext(New System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"))

    Dim tblEmps As Table(Of Emp) = db.GetTable(Of Emp)()

    'getting an exiting row

    Dim objEmp As Emp = tblEmps.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

    'getting table

    Dim db As New DataContext(New System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"))

    Dim tblEmps As Table(Of Emp) = db.GetTable(Of Emp)()

    '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}

    tblEmps.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


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

    Try

    'getting table

    Dim db As New DataContext(New System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"))

    Dim tblEmps As Table(Of Emp) = db.GetTable(Of Emp)()

    'getting an exiting row

    Dim objEmp As Emp = tblEmps.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


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

    Try

    'getting table

    Dim db As New DataContext(New System.Data.SqlClient.SqlConnection("data source=.sql2k5;initial catalog=Sample;user id=sa;Password=eXpress2005"))

    Dim tblEmps As Table(Of Emp) = db.GetTable(Of Emp)()

    'getting an exiting row

    Dim objEmp As Emp = tblEmps.SingleOrDefault(Function(p) p.empno = Me.txtEmpno.Text)

    If objEmp IsNot Nothing Then

    'delete the row

    tblEmps.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


    Once executed, it should give the output as follows (Fig 06):


    More .NET Articles
    More By Jagadish Chaterjee


       · Hello guys,I am back with a series on "LINQ to SQL" using Visual Studio 2008. ...
       · WHat is that in new LINQ in VS2008.?Can any one explain it..............?
     

    .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...





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