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

How to use LinqDataSource in ASP.NET

Author: Rudy Pinson
Download Source Code : 1288_LinqDataSource.zip

One way to accomplish LINQ to SQL is by using LinqDataSource. LinqDataSource Control provides easy way to populate GridView or detail View from SQL database.

 

LINQ is explained in a previous article on DevASP.NET. If you want to know what LINQ is, refer to my article.

We can easily insert, update and delete data by using LinqDataSource. Configuration of LinqDataSource is also easy. You have to add LINQ to SQL Classes to use LinqDataSource.
 
Below is example to use LinqDataSource. You can also download source code. 
 
  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 Design View. Drag and Drop a LinqDataSource and configure it from little arrow at the top. DataClassesDataContext will be our context object. Next you can select columns you want to show. Source view of Default.aspx page will be like this.

    <asp:LinqDataSource ID="LinqDataSource1" runat="server"
       
           ContextTypeName="DataClassesDataContext" EntityTypeName="" TableName="Products">
       
    </asp:LinqDataSource>

     
  7.  Drag and Drop a GridView and choose LinqDataSource1. Source View will be as under:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
       
              AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductID"
       
              DataSourceID="LinqDataSource1">
       
           <Columns>
       
               <asp:BoundField DataField="ProductID" HeaderText="ProductID"
       
                        InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
       
               <asp:BoundField DataField="ProductName" HeaderText="ProductName"
       
                        SortExpression="ProductName" />
       
               <asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
       
                        SortExpression="SupplierID" />
       
               <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
       
                        SortExpression="CategoryID" />
       
               <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
       
                        SortExpression="QuantityPerUnit" />
       
               <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
       
                        SortExpression="UnitPrice" />
       
               <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
       
                        SortExpression="UnitsInStock" />
       
               <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
       
                        SortExpression="UnitsOnOrder" />
       
               <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
       
                        SortExpression="ReorderLevel" />
       
               <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
       
                        SortExpression="Discontinued" />
       
           </Columns>
       
       </asp:GridView>
     
  8. You can also insert, update and delete using LinqDataSource. Drag and Drop another LinqDataSource in Default.aspx page and configure it. Source View will be like this.

    <asp:LinqDataSource ID="LinqDataSource2" runat="server"
       
          ContextTypeName="DataClassesDataContext" EnableDelete="True"
       
          EnableInsert="True" EnableUpdate="True" EntityTypeName="" TableName="Products">
       
    </asp:LinqDataSource>

     
  9.  Now drag and drop a DetailView this time and choose LinqDataSource2. Source code is as under:

    <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
       
              AutoGenerateRows="False" DataKeyNames="ProductID"
       
              DataSourceID="LinqDataSource2" Height="50px" Width="243px">
       
          <Fields>
       
              <asp:BoundField DataField="ProductID" HeaderText="ProductID"
       
                        InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
       
              <asp:BoundField DataField="ProductName" HeaderText="ProductName"
       
                        SortExpression="ProductName" />
       
              <asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
       
                        SortExpression="SupplierID" />
       
              <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
       
                        SortExpression="CategoryID" />
       
              <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
       
                        SortExpression="QuantityPerUnit" />
       
              <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
       
                        SortExpression="UnitPrice" />
       
              <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
       
                        SortExpression="UnitsInStock" />
       
              <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
       
                        SortExpression="UnitsOnOrder" />
       
              <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
       
                        SortExpression="ReorderLevel" />
       
              <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
       
                        SortExpression="Discontinued" />
       
              <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
       
                        ShowInsertButton="True" />
       
          </Fields>
       
     </asp:DetailsView>
     
  10.  View the Web Site in browser and see the result.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to upload multiple files using jQuery in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net