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.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : 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 April 5th, 2015, 12:43 PM
PSK PSK is offline
Registered User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Warwickshire, England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 14, first try it out - Compilation Error Planet Wrox - Entity Framework

Hello,

I'm trying to learn ASP.NET, so I've decided to work through a book - beginning asp.net 4.5 in c# and vb - I'm on page 548, but now I'm stuck.

I've tried to read every word, so as not to miss anything, and gone through all exercises, but I have received an error and can't work out why.

The error is:

Line 1: using PlanetWroxModel;
Line 2: using System;
Line 3: using System.Collections.Generic;

The book states it may come up and how to fix it, but one of the options wasn't in visual studio.

From book

"Notice how you immediately get an error when you type PlanetWroxEntities because it’s defined in a namespace that is not in scope. You can fi x the problem in two ways. You can type Imports PlanetWroxModel if you’re using VB.NET or using PlanetWroxModel; if you’re using C# at the top of the code fi le. Alternatively, you can click the word PlanetWroxEntities once and then press Ctrl+. (Ctrl+Dot) to bring up a dialog box that lets you choose the fi x for the problem, shown in Figure 14-3 for the C# language.
Choose the fi rst item and VS adds the necessary Imports/using statement for you. In C# you can also right-click the word PlanetWroxEntities and then click the Resolve menu to bring up a similar menu"


I tried the suggestions, but still no joy.

Here is my code:

Code:
using PlanetWroxModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Reviews_All : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
        {
            var authorizedReviews = from review in myEntities.Reviews
                                    where review.Authorized == true
                                    orderby review.CreateDateTime descending
                                    select review;
            GridView1.DataSource = authorizedReviews;
            GridView1.DataBind();
        }
    }
}
Please can someone help.

Last edited by PSK; April 5th, 2015 at 12:45 PM..
Reply With Quote
  #2 (permalink)  
Old April 5th, 2015, 03:54 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Can you post the full error message? You now just posted the first few lines of the code that triggered the exception.

Are you by any chance using Visual Studio 2013 instead of 2012 that the book targets?

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 April 6th, 2015, 07:36 AM
PSK PSK is offline
Registered User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Warwickshire, England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply,

Yes, I am using 2013 - would this cause the issue?

Also,on page 502, step 7, it says you should change the 'Code Generation Strategy' from None to Default, but Visual Studio displays only 'T4', and it's greyed out.

Step 8 ask you to "Save and close the diagram and then delete the fi les PlanetWrox.tt and PlanetWrox.Context.tt from the App_Code folder". As a test I decided not to delete these files, but got the error message :

Quote:
Server Error in '/' Application.

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) 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(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.

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, DbRawSqlQuery) 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(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.

Source Error:


Line 17: select review;
Line 18: GridView1.DataSource = authorizedReviews;
Line 19: GridView1.DataBind();
Line 20: }
Line 21: }
After following the steps I get this:

Quote:
Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'PlanetWroxModel' could not be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 1: using PlanetWroxModel;
Line 2: using System;
Line 3: using System.Collections.Generic;

Source File: c:\BegASPNET\Site\Reviews\All.aspx.cs Line: 1
After adding the 'using PlanetWroxModel;' my code still seems to get the red squiggles - under the top using line, using(line 14) and the last Reviews word (line 16). I have put them in bold.

Code:
using PlanetWroxModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 

public partial class Reviews_All : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
        {
            var authorizedReviews = from review in myEntities.Reviews
                                    where review.Authorized == true
                                    orderby review.CreateDateTime descending
                                    select review;
            GridView1.DataSource = authorizedReviews;
            GridView1.DataBind();
        }
    }
}

Last edited by PSK; April 6th, 2015 at 08:54 AM..
Reply With Quote
  #4 (permalink)  
Old April 6th, 2015, 08:45 AM
PSK PSK is offline
Registered User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Warwickshire, England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, I think it's very cool that I'm speaking with the author of the book!
Reply With Quote
  #5 (permalink)  
Old April 6th, 2015, 02:52 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

>> Yes, I am using 2013 - would this cause the issue?

Yes, that is an issue in this case. VS 2013 uses Entity Framework 6 by default which has a different API and behavior than version 5. This post provides more details and a solution:

Your Book and VS Versions

In addition, your first error may be fixed by calling ToList() on the authorizedReviews collection (can't tell from your post whether you already tried that).

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
  #6 (permalink)  
Old April 8th, 2015, 01:38 PM
PSK PSK is offline
Registered User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Warwickshire, England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

Thanks for the reply. Please excuse the slow reply from me.

Not that this is the way to go, but I assume it would be possible to change the version of the Entity Framework to 5, should I be unable to correct the problem?

As for calling ToList() - I don't know where to do this? I'm guessing it's in code behind file of the all.aspx page, but I don't exactly know where. I'm a total beginner and could do with as much help as possible.

Code:
using PlanetWroxModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 

public partial class Reviews_All : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
        {
            var authorizedReviews = from review in myEntities.Reviews
                                    where review.Authorized == true
                                    orderby review.CreateDateTime descending
                                    select review;
            GridView1.DataSource = authorizedReviews;
            GridView1.DataBind();
        }
    }
}
I'll read through the link you sent across.

Cheers once again.
PSK

Last edited by PSK; April 8th, 2015 at 02:02 PM..
Reply With Quote
  #7 (permalink)  
Old April 9th, 2015, 03:51 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

You can't change the version of EF. However, you can uninstall version 6 and then reinstall version 5 which comes down to the same thing. The post I linked to has the details.

About calling ToList, just change this:

GridView1.DataSource = authorizedReviews;

to this:

GridView1.DataSource = authorizedReviews.ToList();

This causes the authorizedReviews query to execute and return actual reviews to the GridView instead of return the query itself.

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
  #8 (permalink)  
Old April 10th, 2015, 01:58 PM
PSK PSK is offline
Registered User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Warwickshire, England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

Thanks for the reply.

I've been reading through the links you sent, but I don't understand model binding. I'll continue to read. I got the book you recommended about Entity Framework by Julia Lerman, but that is probably a step too soon I would imagine.

I'll try reading the article again, but if I struggle, I'll go with suggestion 1.

Thanks again,
PSK
Reply With Quote
  #9 (permalink)  
Old April 10th, 2015, 02:27 PM
PSK PSK is offline
Registered User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Warwickshire, England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

I tried adding ToList(), but it still came back with the same error.

I went through the article but when I tried to open the .mdf file in the Server Explorer, it wouldn't open - I don't think I was doing something incorrectly.

I'll delete everything and try using the EF version 5 method.

Thanks,

PSK
Reply With Quote
  #10 (permalink)  
Old April 10th, 2015, 02:45 PM
PSK PSK is offline
Registered User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Warwickshire, England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

I deleted everything as per your suggestion and then tried it again, but this time using EF 5. Unfortunately, the same error message popped up as before - red squiggly lines under the top using PlantWroxModel line, using and the last Reviews word. I have put them in bold


Code:
using PlanetWroxModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 

public partial class Reviews_All : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
        {
            var authorizedReviews = from review in myEntities.Reviews
                                    where review.Authorized == true
                                    orderby review.CreateDateTime descending
                                    select review;
            GridView1.DataSource = authorizedReviews;
            GridView1.DataBind();
        }
    }
}

Any idea why this might be happening?

As always thanks for your replies and time. Greatly appreciated.

Regards
PSK

Last edited by PSK; April 10th, 2015 at 02:51 PM..
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
Chapter 13 - operating system error 5: "5(access is denied.)" for Planet Wrox DB louisemcdonald1@gmail.com BOOK: Beginning ASP.NET 4.5.1 : in C# and VB 4 March 28th, 2015 07:02 PM
PLEASE HELP WITH THE ERROR IN Chapter 14 LINQ and the ADO.NET Entity Framework Tobi BOOK: Beginning ASP.NET 4 : in C# and VB 2 February 19th, 2014 08:03 AM
Chapter 14 - Using Data Controls with the Entity Framework nudirection BOOK: Beginning ASP.NET 4.5 : in C# and VB 3 July 11th, 2013 02:29 AM
Chapter 14 Linq and ado.net entity framework pandukal21 BOOK: Beginning ASP.NET 4 : in C# and VB 8 January 28th, 2011 05:19 AM
Wrox United - chapter 14 - error SqlDependency tom_10000001 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 November 10th, 2006 06:22 AM



All times are GMT -4. The time now is 11:28 AM.


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