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

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 / 13
    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


    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..............?
       · i downloaded source code and could not find tables, "emp" and "dept". am i missing...
       · since i'm so new to this i have to ask a stupid question. i downloaded the source...
       · You can find it...
       · You are supposed to create those tables by yourself in your own database. The...
       · thank you very much!! i knew i was missing something.
       · Nice article. it was very useful for me to learn how to work with LINQ
     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek