Basic ASP.NET application using Entity Framework

by Jagadish Pulakhandam on 4/23/2012 11:54:38 AM
Rated 0 from 0 votes
Brief: Demonstrates on connecting/fetching SQL Server data using Entity Framework and ASP.NET
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 connect to SQL Server using Entity Framework
  • How to create (basic) Entity Data Model (entities) using Entity Framework Designer (and wizard)
  • How to configure/use EntityDataSource to work with Entity Data Model
  • How to bind GridView to EntityDataSource

NOTE: uses "Northwind" database (you have to modify connection string or connection parameters according to your environment).

Source Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Sample01.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"
            DataSourceID="EntityDataSource1">
            <Columns>
                <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"
                    SortExpression="OrderID" />
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
                    SortExpression="CustomerID" />
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True"
                    SortExpression="EmployeeID" />
                <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" ReadOnly="True"
                    SortExpression="OrderDate" />
                <asp:BoundField DataField="Freight" HeaderText="Freight" ReadOnly="True"
                    SortExpression="Freight" />
                <asp:BoundField DataField="ShipCity" HeaderText="ShipCity" ReadOnly="True"
                    SortExpression="ShipCity" />
            </Columns>
        </asp:GridView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server"
            ConnectionString="name=NorthwindEntities"
            DefaultContainerName="NorthwindEntities" EnableFlattening="False"
            EntitySetName="Orders"
            Select="it.[OrderID], it.[CustomerID], it.[EmployeeID], it.[OrderDate], it.[Freight], it.[ShipCity]">
        </asp:EntityDataSource>
     
    </div>
    </form>
</body>
</html>

Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <connectionStrings>
    <add name="NorthwindEntities" connectionString="metadata=res://*/NorthwindModel.csdl|res://*/NorthwindModel.ssdl|res://*/NorthwindModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\sql2k8;Initial Catalog=Northwind;User ID=sa;Password=sql2008;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>


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