 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 25th, 2011, 01:26 AM
|
Registered User
|
|
Join Date: Jan 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Chapter 14 Linq and ado.net entity framework
Page 510, chapter 14, after adding the following code in the event handler, following error is thrown.
'PlanetWroxModel.Picture' does not contain a definition for 'PhotoAlbumId' and no extension method 'PhotoAlbumId' accepting a first argument of type 'PlanetWroxModel.Picture' could be found (are you missing a using directive or an assembly reference?)
My ManagePhotoAlbum.aspx.cs is as follows.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;
public partial class _ManagePhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
Picture myPicture = (Picture)e.Entity;
myPicture.PhotoAlbumId = photoAlbumId;
}
}
kindly Help..!!!
Thanks in advance...  
|

January 25th, 2011, 03:41 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
When you created the model in Step 4 on page 499, did you leave the option "Include foreign key columns in the model" checked? You need that option in order for the AlbumId to show up.
Also, check that you created the correct relationship between the tables as explained in the same exercise before you add the tables to the model.
Hope this helps,
Imar
|

January 25th, 2011, 06:03 AM
|
Registered User
|
|
Join Date: Jan 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
I checked out the same, but "Include Foreign key Columns in the model" check box is not reflecting in the "Entity Data Model Wizard". I'm using VWD xpress edition 2008. How to enable/display the same..?
rgds,
pandukal21
|

January 25th, 2011, 06:06 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You need VWD or VS 2010 to follow along with this book. The foreign key properties were introduced in EF 4 which ships with .NET 4 / VS 2010, and not with .NET 3.5 / VS 2008.
Cheers,
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

January 25th, 2011, 06:25 AM
|
Registered User
|
|
Join Date: Jan 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Managed to reach till chapter 14 with VWD 2008  . Anyways, thank you so much for your information..
Rgds,
pandukal21
|

January 26th, 2011, 06:01 PM
|
Registered User
|
|
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Facing The Same problem
Kindly,the same problem faced me when I tried to write that code in the code behind page "ManagePhotoAlbum.aspx" when I tried to write the statement
"mypicture.PhotoAlbumId=photoAlbumId"
but VWD didn't recognize that forign key PhotoAlbumId
really I'm using VWD Express 2008 with .Net Framework 3.5 .
In that case Mr/Imar What the code could be if I using VWD2008/EF3.5 not VWD2010/EF4 as in the book to recover that problem.
The remainder of the book is fine with me using that version of VWD
Waiting your reply.......
Thanks in Advance
|

January 26th, 2011, 06:56 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're probably better off downloading the free Express Edition of VWD 2010. You'll run into other problems later as well
Anyway, since there's no PhotoAlbumId, you need to query an actual instance. E.g. something like:
Quote:
using (PlanetWroxContext context = new PlanetWroxContext())
{
...
myPicture.PhotoAlbum = context.PhotoAlbums.Where(a => a.Id == photoAlbumId).First();
}
|
Hope this helps,
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

January 27th, 2011, 10:47 AM
|
Registered User
|
|
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
RunTime Error
--I had written the code in the page "ManagePhotoAlbum.aspx" as follows:
Imports System.Linq
Imports PlanetWroxModel
Partial Class _ManagePhotoAlbum
Inherits BasePage
Protected Sub EntityDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangedE ventArgs) Handles EntityDataSource1.Inserted
Dim a As New PhotoAlbum
Dim Context As New PlanetWroxEntities1
Dim photoAlbumId As Integer = _
Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"))
Dim myPicture As Picture = CType(e.Entity, Picture)
myPicture.PhotoAlbum = Context.PhotoAlbum.Where(a.Id = photoAlbumId).First()
End Sub
End Class
and when I requested the page in the browser and entered the required Description,ToolTip, and ImageURL for the picture and clicked Insert,I got the following RunTime Error:-
Object Reference not set to an instance of an Object
for the line
myPicture.PhotoAlbum = Context.PhotoAlbum.Where(a.Id = photoAlbumId).First()
What the error can be
--In your code in the last reply,you wrote a line in the code:-
myPicture.PhotoAlbum = context.PhotoAlbums.Where(a => a.Id == photoAlbumId).First();
Kindly,What a refers to?
Waiting your Reply......
Thanks in Advance
|

January 28th, 2011, 05:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
This is a range variable. See page 488 for more details.
Sorry, the example I gave was in C#. I also found some other issues that needed to be fixed. In VB you can do something like this:
Code:
Using myContext As New PlanetWroxEntities()
Dim photoAlbum As PhotoAlbum = myContext.PhotoAlbums.Where(Function(album) album.Id = photoAlbumId).First()
myContext.Detach(photoAlbum)
e.Context.AttachTo("PhotoAlbums", photoAlbum)
myPicture.PhotoAlbum = photoAlbum
End Using
This queries the album from the new context, detaches it and then reattaches it to the existing context.
You can also avoid calling the database by creating a fake PhotoAlbum and attaching that:
Code:
Dim photoAlbum As PhotoAlbum = New PhotoAlbum() With {.Id = photoAlbumId}
e.Context.AttachTo("PhotoAlbums", photoAlbum)
myPicture.PhotoAlbum = photoAlbum
BTW, you are calling this code in the Inserted event, while you should be using the Inserting event.
Cheers,
Imar
Last edited by Imar; January 29th, 2011 at 01:15 PM..
Reason: Removed Using block from the second sample
|
|
 |
|