LinqDataSource Example in ASP.NET using C#

LinqDataSource Example in ASP.NET using C#


Posted in : ASP.Net Posted on : December 11, 2010 at 6:09 PM Comments : [ 0 ]

In this article we will introduce with LinqDataSource control in ASP.NET using C#.

LinqDataSource Example in ASP.NET using C#

The LinqDataSource control is a data bound control. LinqDataSource similar like SqlDataSource, ObjectDataSource and EntityDataSource. You create web pages that are enable users to update, insert and delete data. In this example we use a GridView and provide data using LinqDataSource. Now we will have to need Linq to Sql class for configure LinqDataSource.
There are the following step for creating Linq to Sql class.

Step 1. Add Linq To Sql Class in Solution Explorer.

Step 2. Now it will show a message you will click yes button.

Step 3. Now click DataBase explorer and drag and drop the table on Linq to Sql class.

Step 4.The class will be shown in App_Code folder.

Now Drag and drop a GridView and LinqDataSource in your web page. There are the following steps to configure LinqDataSource with GridView.

Step 1. Click smart tag button and select <New data Source> from Choose Data Source dropdownlist. Now select Linq and click OK.

Step 2. Now Choose a Context Object and click Next.

Step 3. Configure Data Selection and click Advanced button.

Step 4. Enable insert, update and delete and click OK then finish.

Now click smart tag button of GridView and enable paging, sorting, editing, deleting and selection.

LinqDataSource.aspx (Design Page):

LinqDataSource.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true"
CodeFile="LinqDataSource.aspx.cs" Inherits="LinqDataSource" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
LinqDataSource in ASP.NET 4, C#</h2>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ProductID" 
DataSourceID="LinqDataSource1" ForeColor="#333333" GridLines="None" 
PageSize="4"
>
<AlternatingRowStyle BackColor="White"/font> />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
ShowSelectButton="True" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" 
SortExpression="ProductName" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
ContextTypeName="LinqToSqlClassDataContext" EnableDelete="True" 
EnableInsert="True" EnableUpdate="True" EntityTypeName="" TableName="Products">
</asp:LinqDataSource>
</div>
</asp:Content>

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics