Using DLINQ with ASP.NET (Part 2 of my LINQ series)
page 5 of 10
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 38643/ 96

Step 3: Build a page that uses our DLINQ layer

To start off with, we’ll create a new page within our project called “Sample1.aspx”. 

Within the Sample1.aspx file we’ll add a GridView server control and explicitly define a few columns to display:

Listing 6

<%@ Page Language="C#" CodeFile="Sample1.aspx.cs" Inherits="Sample1" %>
<html>
<body>
    <form id="form1" runat="server">
    <h1>Northwind Customers</h1>   
    <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
       <Columns>
          <asp:BoundField HeaderText="Customer ID" DataField="CustomerID" />
          <asp:BoundField HeaderText="Name" DataField="CompanyName" />
          <asp:BoundField HeaderText="City" DataField="City" />
          <asp:BoundField HeaderText="State" DataField="Region" />
       </Columns>
    </asp:GridView> 
    </form>
</body>
</html>

Within the Sample1.aspx.cs code-behind file we’ll add a “using System.Query” statement at the top of the file, and then add LINQ code within the Page_Load() event to retrieve all US customers from the Northwind database, sorted by CompanyName, and programmatically bind the results to the GridView control:

Listing 7

using System;
using System.Configuration;
using System.Web.UI;
using System.Query;
public partial class Sample1 : System.Web.UI.Page {
    protected void Page_Load() {
        string connectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
        Northwind.Northwind db = new Northwind.Northwind(connectionString);
        GridView1.DataSource = from customer in db.Customers
                               where customer.Country == "USA"
                               orderby customer.CompanyName
                               select customer;
 
        GridView1.DataBind();
    }
}

When we save the page and access it from a browser we’ll get this output:

Figure 3

Disclaimer: I will leave it as an exercise to the reader to write a nice CSS stylesheet to make it look pretty – but hopefully you get the point of how the functionality works. J


View Entire Article

User Comments

Title: DLINQ Usage   
Name: Subhashini
Date: 2010-12-18 1:36:06 AM
Comment:
This article solved many of questions about DLINQ.I understand how to do create database structures for DLINQ query using Attribute Based Mapping and through XML Based Mapping.
Title: Helping hand for the DLINQ users   
Name: Tejaswini Jangale-Chaudhari
Date: 2009-04-22 1:02:16 AM
Comment:
The article is real nice, n helped me a lot to understand binding and pagination. Keep posting such helpful articles :)
Title: Great Article   
Name: Yuna
Date: 2008-07-18 3:42:52 AM
Comment:
thank you very much, my article help me understand LINQ
Title: Great Article   
Name: Jaykumar Acharya
Date: 2008-06-18 9:33:59 AM
Comment:
I must say you are my guru of LINQ. These posts really helped me to learn LINQ and DLINQ. It was like a cake walk to learn deep concepts. Please keep posting such blogs for us.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-02 1:12:52 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search