Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

LINQ to DataSet in ASP.NET

Author: Rudy Pinson
Download Source Code :

DataSet is the great in-memory representation of data. LINQ to DataSet allows you to write queries for DataSet in your application.


LINQ to SQL allows queries to get data from SQL Server databases. LINQ to DataSet provides broad query capabilities in your application for disconnected data. Developers can get a copy of data and perform queries and write the result back to the database. LINQ to DataSet supports both typed and un-typed DataSets.

Below is an example for LINQ to DataSet. Source code for the example is available to download at the top.


  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Now make connection to the server by using server explorer. I am using NORTHWIND Sample Database for this example.
  4. Add LINQ to SQL Classes by using Add New Item. It will be added in App_Code folder.
  5. Drag a Table from Server Explorer table list of your database and drop it in DataClasses.dbml. I am using “Products” table from NORTHWIND sample database.
  6. Now open Default.aspx page and drag and drop a GridView in the page.

    div><asp:GridView ID="GridView1" runat="server">
  7. Open code behind file and write code below in Page Load method. Make sure “System.Linq” namespace exists at the top of your code behind file.

    using System.Linq;
    using System.Data;
    using System.Data.SqlClient;

    protected void Page_Load(object sender, EventArgs e) {
        DataClassesDataContext db = new DataClassesDataContext();
        string sql = "Select * From Products";
        SqlDataAdapter da = new SqlDataAdapter(sql, db.Connection.ConnectionString);
        DataTable dt = new DataTable();
        var products = from pro in dt.AsEnumerable()
                       where pro.Field("UnitPrice") <= 10
                       select pro.Field("ProductName");
        GridView1.DataSource = products;

    Imports System.Linq
    Imports System.Data
    Imports System.Data.SqlClient

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim db As New DataClassesDataContext()
        Dim sql As String = "Select * From Products"
        Dim da As New SqlDataAdapter(sql, db.Connection.ConnectionString)
        Dim dt As New DataTable()
        Dim products = From pro In dt.AsEnumerable()
                       Where pro.Field(Of Decimal)("UnitPrice") <= 10
                       Select pro.Field(Of String)("ProductName")
        GridView1.DataSource = products
    End Sub
    I have used SQL query to get data from database then pass this query statement and connection string to SqlDataAdapter. DataTable is filled using this SqlDataAdapter. We have to convert our DataTable to Enumerable to write LINQ to DataSet query. We have to cast our column to appropriate type to use it in the query.
  8. View the Web Site in browser and see the result. 


Add Article Comment:
Name :
Email Address :
Comments :
<< How to Backup SQL Server Database in ASP.NET

Disclaimer - Privacy
© 2002-2018