News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  May 30 2015 9:54PM     Micheal
  0 Comments    4605 Views  
Here Micheal provided a short tutorial with example, how to use LINQ to SQL classes in ASP.Net C#.
What is LINQ ?
  1. Language Integrated Query or LINQ is the collection of standard query operators which provides query facilities into.NET framework language like C#, VB.NET.
  2. LINQ is required as it bridges the gap between the world of data and world of objects.
Image given below shows structure of LINQ Query:
Steps to Create .dbml (LINQ to SQL Classes)
Step 1: Open Visual Studio and Create ASP.Net Web Application.
Step 2: Right click the solution and Select Add New Item.. select Linq to sql classes option from the list
Step 3: The image given belows shows to Select Linq to sql classes in Add New Item Popup Window

Step 4: After adding .dbml file, your solution explorer will look like this,
Step 5: Double click the DataContext.dbml to add Database tables.
Step 6: Open Server Explorer and Click "Connect to Database", as shown in below image
Step 7: After entering all the details, Click Ok
Step 8: Drag and Drop the tables from the Database, After placing tables your DataContext.dbml file will look like this.
Step 9: Right click the DataContext.dbml file and click properties. Change the Name as "DataContext" and remove the Connection string. Image given below shows that,
Step 10: Just save the file and close.
Step 11: Open ASP.Net page write the following code.
Below code shows, Binding gridview using LINQ Query.
Default.aspx
HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2><b><div>The Developer Tips</div></b></h2>
    
    <h3><b>Gridview with LINQ</b></h3>

    <asp:GridView ID="gridviewEmp" runat="server">
    </asp:GridView>
    </div>
    </form>
</body>
</html>

C#

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void BindGridView()
        {
            using (DataContext objDataContext = new DataContext())
            {
                var result = from c in objDataContext.Employees
                             where c.EmployeeAge >= 20
                             select c;                

                List<Employee> lstEmployee = result.ToList();
                gridviewEmp.DataSource = lstEmployee;
                gridviewEmp.DataBind();
            };
        }
    }

Result
I hope this page will helps to create LINQ to SQL Classes in ASP.Net application. Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Nov 21 2017 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: