EntityDataSource Example in ASP.NET using C#

EntityDataSource Example in ASP.NET using C#


Posted in : ASP.Net Posted on : December 10, 2010 at 5:57 PM Comments : [ 0 ]

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

EntityDataSource Example in ASP.NET using C#

In this article we will discuss how to use EntityDataSource in your application. EntityDataSource similar like other data sources as LinqDataSource, SqlDataSource etc. Firstly we have to make a ADO.net Entity Data Model and after that we use this data model in EntityDataSource.
There are the following steps of making ADO.NET Entity data model.

Step 1. Add ADO.NET Entity Data Model in solution explorer. You can see the following figure.

Step 2. It adds in App_code folder (By default). You can see the following figure.

 

Step 3. Now choose model content Generate from Database and click next.

 

Step 4. Now Choose your data connection and click next.

Step 5. Now Choose your Database Objects and click finish.

The connection string for Entity look like that. That are following.

<connectionStrings>
<add name="ChartDatabaseConnectionString" connectionString="Data Source=MSPROGRA\SQLEXPRESS;Initial Catalog=ChartDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ChartDatabaseEntities1" connectionString="metadata=res://*/App_Code.Model.csdl|res://*/App_Code.Model.ssdl|
res://*/App_Code.Model.msl;
provider=System.Data.SqlClient;provider connection string=&quot;Data Source=MSPROGRA\SQLEXPRESS;Initial Catalog=ChartDatabase;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
 

Your database object show look like that.

In this example we use a DataGrid and provide a data using EntityDataSource. Now click on smart tag button of GridView and choose data source the window look like this. Configure ObjectContext and click next.

Now Configure Data Selection and click finish.

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

EntityDataSource.aspx (Design Page):

EntityDataSource.aspx (source code);

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="EntityDataSource.aspx.cs" Inherits="EntityDataSource" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color:Green">EntityDataSource in ASP.NET 4, C#</h2>
<asp:GridView ID="GridView1" runat="server" DataSourceID="EntityDataSource1" 
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
CellPadding="4" DataKeyNames="ProductID" ForeColor="#333333" 
GridLines="None" PageSize="4">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<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="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
ConnectionString="name=ChartDatabaseEntities1" 
DefaultContainerName="ChartDatabaseEntities1" EnableDelete="True" 
EnableInsert="True" EnableUpdate="True" 
EntitySetName="Products">
</asp:EntityDataSource>
<br />
</div>
</asp:Content>
 

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics