ASP.NET Data Binding and CRUD operations using EntityDataSource

by Jagadish Pulakhandam on 5/14/2012 12:57:26 PM
Rated 0 from 0 votes
Brief: Demonstrates on performing CRUD operations using EntityDataSource together with ASP.NET GridView and FormView controls
Posted to: Programming Entity Framework (Basics) - Examples and Videos
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached Sample/Video demonstrates the following:
  • How to create a simple Entity Framework Model from the scratch (just one entity using Northwind database)
  • How to bind EF model to GridView using EntityDataSource (for UPDATE/DELETE operation)
  • How to bind EntityDataSource to FormView (for INSERT operation)
  • How to use FormView.ItemInserted event (to refresh GridView)
  • How to perform CRUD operations using GridView, FormView and EntityDataSource/Entity Framework (with almost no code-behind)


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="CategoryID" DataSourceID="dsrcCategories">
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" ReadOnly="True"
                    SortExpression="CategoryID" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
                    SortExpression="CategoryName" />
                <asp:BoundField DataField="Description" HeaderText="Description"
                    SortExpression="Description" />
        <asp:EntityDataSource ID="dsrcCategories" runat="server"
            DefaultContainerName="NorthwindEntities" EnableDelete="True"
            EnableFlattening="False" EnableInsert="True" EnableUpdate="True"
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="CategoryID"
            DataSourceID="dsrcCategories" DefaultMode="Insert"
                <asp:Label ID="CategoryIDLabel1" runat="server"
                    Text='<%# Eval("CategoryID") %>' />
                <br />
                <asp:TextBox ID="CategoryNameTextBox" runat="server"
                    Text='<%# Bind("CategoryName") %>' />
                <br />
                <asp:TextBox ID="DescriptionTextBox" runat="server"
                    Text='<%# Bind("Description") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
                    CommandName="Update" Text="Update" />
                 <asp:LinkButton ID="UpdateCancelButton" runat="server"
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                <br />
                <asp:TextBox ID="CategoryNameTextBox" runat="server"
                    Text='<%# Bind("CategoryName") %>' />
                <br />
                <asp:TextBox ID="DescriptionTextBox" runat="server"
                    Text='<%# Bind("Description") %>' />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
                    CommandName="Insert" Text="Insert" />
 <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
                    CommandName="Cancel" Text="Cancel" />
                <asp:Label ID="CategoryIDLabel" runat="server"
                    Text='<%# Eval("CategoryID") %>' />
                <br />
                <asp:Label ID="CategoryNameLabel" runat="server"
                    Text='<%# Bind("CategoryName") %>' />
                <br />
                <asp:Label ID="DescriptionLabel" runat="server"
                    Text='<%# Bind("Description") %>' />
                <br />
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
                    CommandName="Edit" Text="Edit" />
                 <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
                    CommandName="Delete" Text="Delete" />
                 <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
                    CommandName="New" Text="New" />


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
    public partial class WebForm1 : System.Web.UI.Page
        protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following