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

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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

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


    (Page 6 of 6 )


    The following is the source code in C# 2008 for the UI designed in the previous sections:


    using System.Data.Linq;


    namespace SampleCS

    {

    public partial class CRUDSample : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    this.lblMsg.Text = string.Empty;

    }


    protected void btnSearch_Click(object sender, EventArgs e)

    {

    try

    {

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

    //getting table

    Table<Emp> tblEmps = db.GetTable<Emp>();

    //getting an existing row

    Emp objEmp = tblEmps.SingleOrDefault(p => p.empno == Int32.Parse( this.txtEmpno.Text ));

    if (objEmp!=null )

    {

    this.txtEname.Text = objEmp.ename;

    this.txtSal.Text = Convert.ToString( objEmp.sal) ;

    this.txtDeptno.Text = Convert.ToString(objEmp.deptno) ;

    }

    else

    this.lblMsg.Text = "Employee not found";

    }

    catch (Exception ex)

    {

    this.lblMsg.Text = ex.Message;

    }

    }


    protected void btnSelectStar_Click(object sender, EventArgs e)

    {

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

    Table<Emp> tblEmps = db.GetTable<Emp>();

    this.GridView1.DataSource = tblEmps;

    this.GridView1.DataBind();

    }


    protected void btnInsert_Click(object sender, EventArgs e)

    {

    try

    {

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

    //getting table

    Table<Emp> tblEmps = db.GetTable<Emp>();

    //adding a new row

    Emp rEmp = new Emp {empno = Int32.Parse(this.txtEmpno.Text), ename = this.txtEname.Text, sal = Double.Parse(this.txtSal.Text), deptno = Int32.Parse(this.txtDeptno.Text)};

    tblEmps.InsertOnSubmit(rEmp);

    //saving rows added

    db.SubmitChanges();

    this.lblMsg.Text = "Added Successfully";

    }

    catch (Exception ex)

    {

    this.lblMsg.Text = ex.Message;

    }

    }


    protected void btnUpdate_Click(object sender, EventArgs e)

    {

    try

    {

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

    //getting table

    Table<Emp> tblEmps = db.GetTable<Emp>();

    //getting an existing row

    Emp objEmp = tblEmps.SingleOrDefault(p => p.empno == Int32.Parse(this.txtEmpno.Text));

    if (objEmp != null)

    {

    //modifying the row

    objEmp.ename=this.txtEname.Text ;

    objEmp.sal = Double.Parse(this.txtSal.Text);

    objEmp.deptno = Int32.Parse(this.txtDeptno.Text);

    //saving rows modified

    db.SubmitChanges();

    this.lblMsg.Text = "Updated Successfully";

    }

    else

    this.lblMsg.Text = "Employee not found";

    }

    catch (Exception ex)

    {

    this.lblMsg.Text = ex.Message;

    }

    }


    protected void btnDelete_Click(object sender, EventArgs e)

    {

    try

    {

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

    //getting table

    Table<Emp> tblEmps = db.GetTable<Emp>();

    //getting an existing row

    Emp objEmp = tblEmps.SingleOrDefault(p => p.empno == Int32.Parse(this.txtEmpno.Text));

    if (objEmp != null)

    {

    //delete the row

    tblEmps.DeleteOnSubmit(objEmp);

    //saving rows modified

    db.SubmitChanges();

    this.lblMsg.Text = "Deleted Successfully";

    }

    else

    this.lblMsg.Text = "Employee not found";

    }

    catch (Exception ex)

    {

    this.lblMsg.Text = ex.Message;

    }

    }

    }

    }

    My upcoming articles will focus further in-depth on "LINQ to SQL" (including designers). I hope you enjoyed this article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - 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...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...
    - Delving Deeper into Serialization with .NET
    - Serialization with .NET

    IBM developerWorks




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