Displaying Master/Detail information using ASP.NET data binding with Entity Framework

by Jagadish Pulakhandam on 5/1/2012 2:32:54 PM
Rated 0 from 0 votes
Brief: Demonstrates on data binding using DropdownList (as parent/master) and GridView (as child/detail) with Entity Framework.
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 an Entity Framework Model with Two related Entities (which are associated with FK in database)
  • How to bind EF model to DropdownList using EntityDataSource
  • How to bind EF model to GridView using EntityDataSource
  • How to filter/apply WHERE condition using EntityDataSource
  • How to display Master/Detail information by relating DropdownList and GridView (as Parent and Child respectively).
Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Sample.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:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            DataSourceID="dsrcCategories" DataTextField="CategoryName"
            DataValueField="CategoryID">
        </asp:DropDownList>
        <asp:EntityDataSource ID="dsrcCategories" runat="server"
            ConnectionString="name=NorthwindEntities"
            DefaultContainerName="NorthwindEntities" EnableFlattening="False"
            EntitySetName="Categories" Select="it.[CategoryID], it.[CategoryName]">
        </asp:EntityDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="dsrcProducts">
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"
                    SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName"
                    ReadOnly="True" SortExpression="ProductName" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" ReadOnly="True"
                    SortExpression="CategoryID" />
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" ReadOnly="True"
                    SortExpression="UnitPrice" />
                <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
                    ReadOnly="True" SortExpression="UnitsInStock" />
            </Columns>
        </asp:GridView>
        <asp:EntityDataSource ID="dsrcProducts" runat="server"
            AutoGenerateWhereClause="True" ConnectionString="name=NorthwindEntities"
            DefaultContainerName="NorthwindEntities" EnableFlattening="False"
            EntitySetName="Products"
            Select="it.[ProductID], it.[ProductName], it.[CategoryID], it.[UnitPrice], it.[UnitsInStock]"
            Where="">
            <WhereParameters>
                <asp:ControlParameter ControlID="DropDownList1" DbType="Int16"
                    Name="CategoryID" PropertyName="SelectedValue" />
            </WhereParameters>
        </asp:EntityDataSource>
     
    </div>
    </form>
</body>
</html>





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