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)

Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="CategoryID" DataSourceID="dsrcCategories">
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>
        <asp:EntityDataSource ID="dsrcCategories" runat="server"
            ConnectionString="name=NorthwindEntities"
            DefaultContainerName="NorthwindEntities" EnableDelete="True"
            EnableFlattening="False" EnableInsert="True" EnableUpdate="True"
            EntitySetName="Categories">
        </asp:EntityDataSource>
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="CategoryID"
            DataSourceID="dsrcCategories" DefaultMode="Insert"
            oniteminserted="FormView1_ItemInserted">
            <EditItemTemplate>
                CategoryID:
                <asp:Label ID="CategoryIDLabel1" runat="server"
                    Text='<%# Eval("CategoryID") %>' />
                <br />
                CategoryName:
                <asp:TextBox ID="CategoryNameTextBox" runat="server"
                    Text='<%# Bind("CategoryName") %>' />
                <br />
                Description:
                <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" />
            </EditItemTemplate>
            <InsertItemTemplate>
                <br />
                CategoryName:
                <asp:TextBox ID="CategoryNameTextBox" runat="server"
                    Text='<%# Bind("CategoryName") %>' />
                <br />
                Description:
                <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" />
            </InsertItemTemplate>
            <ItemTemplate>
                CategoryID:
                <asp:Label ID="CategoryIDLabel" runat="server"
                    Text='<%# Eval("CategoryID") %>' />
                <br />
                CategoryName:
                <asp:Label ID="CategoryNameLabel" runat="server"
                    Text='<%# Bind("CategoryName") %>' />
                <br />
                Description:
                <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" />
            </ItemTemplate>
        </asp:FormView>
     
    </div>
    </form>
</body>
</html>

Code-behind:

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)
        {
            GridView1.DataBind();
        }
    }
}



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
0

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