data binding a aspx web forms dropdownlist to entity framework 6 database first model

Hi experts,

I saw this example on binding a web forms aspx dropdownlist to a Entity Framework 4 Database First model.

ADO.NET Entity Framework 4.0 : DataBinding with ASP.NET
http://blogs.msdn.com/b/wriju/archive/2010/06/03/ado-net-entity-framework-4-0-databinding-with-asp-net.aspx?Redirected=true

At the bottom of that page you can download their example.

So i downloaded their example and changed the web config file to point to my sql server database and ran it and their example worked fine.

One thing I noticed though about that example though is that it is using Entity Framework 4.

So I'm re-creating this example from scratch.
I want to re-create that example using Visual Studio 2013, C#,  .Net Framework 4.5 and Entity Framework 6.0.

So this is what my project looks like.
All the code and naming conventions are the same as in their example.

my Visual Studio 2013 .Net Framework 4 Empty Web Forms Project
This is my code for my page

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>    
    </div>
    </form>
</body>
</html>

Open in new window


WebForm1.aspx.cs

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 Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                using (var ctx = new NorthwindEntities())
                {
                    DropDownList1.DataSource = ctx.Categories;

                    DropDownList1.DataTextField = "CategoryName";
                    DropDownList1.DataValueField = "CategoryID";

                    DropDownList1.DataBind();

                    int cid = (int.Parse(DropDownList1.SelectedValue));

                    GridView1.DataSource = (from p in ctx.Products
                                            where p.CategoryID == cid
                                            select p).ToList();
                    GridView1.DataBind();


                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (var ctx1 = new NorthwindEntities())
            {
                int cid = (int.Parse(DropDownList1.SelectedValue));

                GridView1.DataSource = (from p in ctx1.Products
                                        where p.CategoryID == cid
                                        select p).ToList();
                GridView1.DataBind();
            }
        }
    }
}

Open in new window


My Web.config looks like this:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <connectionStrings>
    <add name="NorthwindEntities" connectionString="metadata=res://*/NW.csdl|res://*/NW.ssdl|res://*/NW.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyServerName;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Open in new window



But when I run my page I get this error:

error I get when i run my page
Anyone know why I am getting this error? Anyone how I fix this error?
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy DownsOWNERCommented:
Maybe this will help. Basically you need you DataSource statements


If you are not interested in getting the reference to the binding list for yourself, and you are using databinding directly against a query as in the sample at the top of this post, you should know that WinForms will call IListSource.GetList() twice, causing the query to be executed on the server twice! The recommendation then is to bind to the results, rather than the query, using code similar to this:
grid.DataSource = someQuery.Execute(MergeOption.AppendOnly);

Open in new window

In this case, since the resulting ObjectResult acts as a foward-only, enumerate-once cursor, its implementation of IListSource.GetList() is different from the one in the query: the call will consume the results (i.e. it will iterate through the whole results) and will cache the binding list in the ObjectResult instance. All subsequent calls to IListSource.GetList() will return the same binding list.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.