.NET
  Home arrow .NET arrow Page 6 - 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 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..............?
       · 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

    - 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 1 Hosted by Hostway
    Stay green...Green IT