Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 4th, 2013, 04:28 PM
Registered User
Points: 35, Level: 1
Points: 35, Level: 1 Points: 35, Level: 1 Points: 35, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2013
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default A simple LINQ to Entities Example (Problem in this Try it out)

Hello all,

First of all, I love this book. I have learned a lot and currently in chapter 14.

I have a problem with the try out in page 481.

I am able to complete all the steps but I am getting an error in the code behind for the Alls.aspx web form.

The code in the book does not work:
-----------------------------------------------------------------
Imports PlanetWroxModel

Partial Class Reviews_All
Inherits BasePage

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using myEntities As New PlanetWroxEntities()
Dim allReviews = From review In myEntities.Reviews
Where review.Authorized = True
Order By review.CreateDateTime Descending
Select review
GridView1.DataSource = allReviews
GridView1.DataBind()
End Using
End Sub
End Class
-----------------------------------------------------------------

The imports works when I use PlanetWroxEntities instead of PlanetWroxModel like this:

Imports PlanetWroxEntities


Even after correcting that line, I get the following error when opening the all.aspx form:

----------------------------------------------------------------------
Server Error in '/Site' Application.
--------------------------------------------------------------------------------


Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().

Source Error:



Line 11: Select review
Line 12: GridView1.DataSource = allReviews
Line 13: GridView1.DataBind()
Line 14: End Using
Line 15: End Sub


Source File: G:\Site\Reviews\All.aspx.vb Line: 13

Stack Trace:



[NotSupportedException: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().]
System.Data.Entity.Infrastructure.DbQuery`1.System .ComponentModel.IListSource.GetList() +57
System.Web.UI.DataSourceHelper.GetResolvedDataSour ce(Object dataSource, String dataMember) +57
System.Web.UI.WebControls.ReadOnlyDataSource.Syste m.Web.UI.IDataSource.GetView(String viewName) +42
System.Web.UI.WebControls.DataBoundControl.Connect ToDataSourceView() +145
System.Web.UI.WebControls.DataBoundControl.GetData () +4
System.Web.UI.WebControls.DataBoundControl.Perform Select() +54
System.Web.UI.WebControls.BaseDataBoundControl.Dat aBind() +30
System.Web.UI.WebControls.GridView.DataBind() +4
Reviews_All.Page_Load(Object sender, EventArgs e) in G:\Site\Reviews\All.aspx.vb:13
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
----------------------------------------------------------------------

It is important to note that I am doing this in Visual Studio 2012 (the book used 2010 so this might be a potential root cause). If this there a way to make this work in 2012?

It will be painful to have to start this WroxPlanet site from scratch in 2010.

I have been looking to fix this problem for one week and have not found anything yet other than some references about how this method does not work with the new versions of the EF.

So good to have found this forum, please let me know your thoughts about this.

Many thanks in advance!
Reply With Quote
  #2 (permalink)  
Old July 4th, 2013, 05:40 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

EF in 2012 works a bit different so the steps won't work one on one between my book and VS 2012. You can try the following which hopefully fixes the problem for you:

After you created / opened the .edmx file, click somewhere on an empty spot of the designer surface and press F4 to open the Properties Grid. Change the Code Generation Strategy property from None to Default.
Next, save and close the diagram and then delete the files PlanetWrox.tt and PlanetWrox.Context.tt from the App_Code folder.

This causes EF to generate different code which should be compatible with the 2010 instructions so the code you posted should then work as advertised.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old July 4th, 2013, 08:00 PM
Registered User
Points: 35, Level: 1
Points: 35, Level: 1 Points: 35, Level: 1 Points: 35, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2013
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Imar, thanks for your quick reply. I recreated the EF configuration again and then changed the property as you recommended. Now I am getting this new error:

-----------------------------------------------------

Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized configuration section entityFramework.

Source Error:



Line 38: </mailSettings>
Line 39: </system.net>
Line 40: <entityFramework>
Line 41: <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbCon nectionFactory, EntityFramework">
Line 42: <parameters>


Source File: G:\Site\web.config Line: 40

-----------------------------------------------------

Code was added automatically on my web.config file, this is the entityFramework section:

----------------------------------------------------------

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbCon nectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>

-----------------------------------------------------

Below is the entire web.config file, not sure what's is going on. Thanks!

<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="PlanetWroxConnectionString1" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirec tory|\PlanetWrox.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="PlanetWroxEntities" connectionString="metadata=res://*/App_Code.PlanetWrox.csdl|res://*/App_Code.PlanetWrox.ssdl|res://*/App_Code.PlanetWrox.msl;provider=System.Data.SqlCl ient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirec tory|\PlanetWrox.mdf;integrated security=True;MultipleActiveResultSets=True;App=En tityFramework&quot;" providerName="System.Data.EntityClient"/>
</connectionStrings>
<system.web>
<pages theme="Monochrome" styleSheetTheme="Monochrome">
<controls>
<add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx"/>
</controls>
</pages>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5">
<buildProviders>
<add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesig nerBuildProvider"/>
</buildProviders>
<assemblies>
<add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5"/>
</system.web>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
<add key="webpages:Enabled" value="true"/>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\TempMail"/>
</smtp>
</mailSettings>
</system.net>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbCon nectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
Reply With Quote
  #4 (permalink)  
Old July 5th, 2013, 03:04 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

It sounds like you have a mix of .NET 4 and 4.5, and different versions of EF. Things you can try:

1. Remove the defaultConnectionFactory or the complete
entityFramework element from the web.config file.

2. Update EntityFramework through the NuGet Package Manager to version 5.

3. Alternatively, start a brand new website, add EF through NuGet, add an ADO.NET model and then compare the resulting web.config file with yours.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old July 5th, 2013, 01:16 PM
Registered User
Points: 35, Level: 1
Points: 35, Level: 1 Points: 35, Level: 1 Points: 35, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2013
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Problem fixed

Hi Imar, good news, problem fixed

Your first suggestion, removing the entityFramework element worked, after saving the file I was able to see my web page with the gridview and all the database data was there

I guess that this fix should allow me to complete the rest of the book exercises, I just have one more database chapter to go (Working with data - Advanced Topics).

Thanks again, you made my day.

Regards from Seattle!
Reply With Quote
  #6 (permalink)  
Old July 6th, 2013, 10:16 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Excellent, glad to hear you got it working.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to concenate strings in LINQ to Entities? IceThatJaw BOOK: Beginning ASP.NET 4 : in C# and VB 1 December 26th, 2011 05:46 AM
Problem inserting entities into XML birchw BOOK: Beginning XML, 4th Ed ISBN: 978-0-470-11487-2 0 December 20th, 2011 06:23 PM
Problem with character entities mister_tea BOOK: Beginning HTML, XHTML, CSS, and JavaScript 2 April 6th, 2010 09:11 AM
Linq Question about related entities gspro BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 March 23rd, 2010 09:29 AM
Comparing Performance Linq To entities and outofBand SQL updates urpcor BOOK: Professional ADO.NET 3.5 with LINQ and the Entity Framework ISBN: 978-0-470-22988-0 4 September 28th, 2009 06:22 AM



All times are GMT -4. The time now is 07:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.