maqskywalker
asked on
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.
This is my code for my page
WebForm1.aspx
WebForm1.aspx.cs
My Web.config looks like this:
But when I run my page I get this error:
Anyone know why I am getting this error? Anyone how I fix this error?
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.
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>
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();
}
}
}
}
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="data source=MyServerName;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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>
But when I run my page I get this error:
Anyone know why I am getting this error? Anyone how I fix this error?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.