Blue Theme Orange Theme Green Theme Red Theme
 
HeaderAd
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET 2.0/3.5 » EntityDataSource Control in ASP.NET 3.5

EntityDataSource Control in ASP.NET 3.5

In this article I am going to discuss how to use EntityDataSource control step by step.

Author Rank:
Technologies: Database, ASP.NET 3.5,Visual C# .NET
Total downloads : 102
Total page views :  9426
Rating :
 5/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
EntityDataSource.zip
 
ArticleAd
Become a Sponsor




In this article I am going to discuss how to use EntityDataSource control step by step. EntityDataSource is a new Data Source control for ASP.Net 3.5 like other controls SqlDataSource, LinqDataSource, ObjectDataSource which makes declaratively binding ASP.NET UI controls to Entity Data Models very easy.

First of all create a simple Web Application, and drag and drop EntityDataSource and Grid View controls on page.

Let's start by diagrams:

Figure1.

Add a new ADO.NET Entity Data Model in solution explorer.

Figure2.

By default it adds in App_Code folder.

Figure3.

Now choose model contents, select Generate from database and click next.

Figure4.

Choose You Data Connection and click next.

Figure5.

By default it saves connection string in configuration file.

<connectionStrings><add name="VendorEntities" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=Vendor;Persist Security Info=True;User ID=sa;Password=wintellect;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /><add name="VendorEntities1" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=Vendor;Persist Security Info=True;User ID=sa;Password=wintellect;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

Choose Your Database Objects now and click next:

Figure6.

After click next it show diagram of your database object like this.

Figure7.

Now time to configure EntityDataSource control.

Figure8.

Configure ObjectContext and Click Next.

Figure9.

Configure Data Selection and click next.

Figure10.

Choose you data source id  for GridView Data Source. You can enable Sorting, Deleting, Selection, Editing, Paging.

Figure11.

On Page it looks like this:

<form id="form1" runat="server">

    <div>

    <h1>EntityDataSource Example</h1>

            <asp:GridView ID="GridView1" runat="server" AllowPaging="True"

                AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"

                DataKeyNames="VendorId" DataSourceID="EntityDataSource1" ForeColor="#333333"

                GridLines="None">

                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

                <Columns>

                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"

                        ShowSelectButton="True" />

                    <asp:BoundField DataField="VendorId" HeaderText="VendorId" ReadOnly="True"

                        SortExpression="VendorId" />

                    <asp:BoundField DataField="VendorFName" HeaderText="VendorFName"

                        SortExpression="VendorFName" />

                    <asp:BoundField DataField="VendorLName" HeaderText="VendorLName"

                        SortExpression="VendorLName" />

                    <asp:BoundField DataField="VendorCity" HeaderText="VendorCity"

                        SortExpression="VendorCity" />

                    <asp:BoundField DataField="VendorState" HeaderText="VendorState"

                        SortExpression="VendorState" />

                    <asp:BoundField DataField="VendorCountry" HeaderText="VendorCountry"

                        SortExpression="VendorCountry" />

                    <asp:BoundField DataField="PostedDate" HeaderText="PostedDate"

                        SortExpression="PostedDate" />

                    <asp:BoundField DataField="VendorDescription" HeaderText="VendorDescription"

                        SortExpression="VendorDescription" />

                </Columns>

                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

                <EditRowStyle BackColor="#999999" />

                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

            </asp:GridView>

            <br />

            <asp:EntityDataSource ID="EntityDataSource1" runat="server"

                ConnectionString="name=VendorEntities" DefaultContainerName="VendorEntities"

                EnableDelete="True" EnableInsert="True" EnableUpdate="True"

                EntitySetName="Vendor">

            </asp:EntityDataSource>

    </div>

    </form>

Figure12.

When you click on Select link button, It will select automatically, like this:

Figure13.

When you click on Edit, will looks like this.

That is it. I did not write one line of code and you can do whatever you want do with GridView.

Download attached sample project for more details.

Cool control.

 


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Rajkumar Beniwal
Raj working as a senior software engineer has 4 years experience working on ASP.NET 2.0, VB.NET,C#, AJAX,SQL and other latest technologies. He holds Master's degree in Computer Science. currently using WPF, WCP, silverlight.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
EntityDataSource.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
good articlereena8/14/2008
good article i was searching exact same thing. this is very helpful for me. thanks alot. keept it up
Reply | Email | Delete | Modify | 
good articlereena8/14/2008
good article i was searching exact same thing. this is very helpful for me. thanks alot. keept it up
Reply | Email | Delete | Modify | 
good workreena8/14/2008
good article i was searching exact same thing. this is very helpful for me. thanks alot. keept it up
Reply | Email | Delete | Modify | 
When would you use an EntityDataSource instead of a SQLDataSource or a LinqDataSource?Michael12/30/2008
Thanks, I always enoy your articles.
Reply | Email | Delete | Modify | 
Question on deleting rowsMatt1/2/2009
When I delete a row from the gridview and then hit the refresh page, an exception is thrown because the object is no longer in the database. I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries. I would think that the Statemanager would be updated. Any suggestions ?
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved