Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : 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
 
Old November 21st, 2016, 01:53 PM
Registered User
 
Join Date: Nov 2016
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 14 Pg514-517

Hi can anyone please help with the following error. I entered the ManagePhotoAlbum.aspx.vb code accordingly but I keep getting the following error. I am not sure where or what I have done wrong to get this error. Please help.

At the bold and underline of the following code.
There is this error stating: 'Pictures' is not a member of 'System.Array'.

The Code:

Code:
Imports System.Web.ModelBinding

Partial Class _ManagePhotoAlbum
    Inherits BasePage

    Public Function ListView1_GetData(<QueryString("PhotoAlbumId")> photoAlbumId As Integer) As IQueryable
        Dim myEntities As PlanetWroxEntities()
        Return From p In myEntities.Pictures
               Where p.PhotoAlbumId = photoAlbumId
               Select p
    End Function

    Public Sub ListView1_InsertItem(<QueryString("PhotoAlbumId")> photoAlbumId As Integer)
        Dim picture As New Picture()
        TryUpdateModel(picture)
        If ModelState.IsValid Then
            Using myEntities As New PlanetWroxEntities()
                picture.PhotoAlbumId = photoAlbumId
                myEntities.Pictures.Add(picture)
                myEntities.SaveChanges()
            End Using
        End If
    End Sub

    ' The id parameter name should match the DataKeyNames value set on the control
    Public Sub ListView1_DeleteItem(ByVal id As Integer)

    End Sub
End Class

Picture.vb
 
Old November 22nd, 2016, 05:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You're missing the New keyword when defining the PlanetWroxEntities. Without New, the () means you're declaring an array of PlanetWroxEntities, not instantiating a new instance of PlanetWroxEntities. Your code should look as follows:

Dim myEntities As New PlanetWroxEntities()

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!
 
Old November 22nd, 2016, 06:28 AM
Registered User
 
Join Date: Nov 2016
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar, again, thank you so much for your quick reply.
After correcting the New instances. I still run into error. I get the following error. Please help.

Expression of type 'Object' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.

The code
Code:
Imports System.Web.ModelBinding

Partial Class _ManagePhotoAlbum
    Inherits BasePage

    Public Function ListView1_GetData(<QueryString("PhotoAlbumId")> photoAlbumId As Integer) As IQueryable
        Dim myEntities As New PlanetWroxEntities()
        Return From p In myEntities.Pictures
               Where p.PhotoAlbumId = photoAlbumId
               Select p
    End Function

    Public Sub ListView1_InsertItem(<QueryString("PhotoAlbumId")> photoAlbumId As Integer)
        Dim picture As New Picture()
        TryUpdateModel(picture)
        If ModelState.IsValid Then
            Using myEntities As New PlanetWroxEntities()
                picture.PhotoAlbumId = photoAlbumId
                myEntities.Pictures.Add(picture)
                myEntities.SaveChanges()
            End Using
        End If
    End Sub

    ' The id parameter name should match the DataKeyNames value set on the control
    Public Sub ListView1_DeleteItem(ByVal id As Integer)

    End Sub
End Class
 
Old November 22nd, 2016, 09:06 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Your code looks fine now, so maybe something is wrong with your PlanetWroxEntities? Is that correctly referring to your EF Model? What do you get when you click on PlanetWroxEntities and then press F12 (Go to definition)?

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!
 
Old November 22nd, 2016, 10:50 AM
Registered User
 
Join Date: Nov 2016
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get the following code:

Code:
Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure

Partial Public Class PlanetWroxEntities
    Inherits DbContext

    Public Sub New()
        MyBase.New("name=PlanetWroxEntities")
    End Sub

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        Throw New UnintentionalCodeFirstException()
    End Sub

    Public Property Genres() As DbSet(Of Genre)
    Public Property Reviews() As DbSet(Of Review)

    Function PhotoAlbums() As Object
        Throw New NotImplementedException
    End Function

    Function Pictures() As Object
        Throw New NotImplementedException
    End Function

End Class
 
Old November 22nd, 2016, 11:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Looks like you incorrectly added the Picture and PhotoAlbum entities. Note how they are defined as a Function. They should be properties similar to how Genres and Reviews are done. I.e.:

Code:
Public Property PhotoAlbums) As DbSet(Of PhotoAlbum)
Try recreating the EF model and see if that fixes it.

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!
The Following User Says Thank You to Imar For This Useful Post:
Lucy75042 (November 22nd, 2016)
 
Old November 23rd, 2016, 09:35 AM
Registered User
 
Join Date: Nov 2016
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar, thanks again. By recreating the EF model. I have managed to get the 2 entries generated. Hence get rid of all the PhotoAlbum and Picture is not an entity of PlanetWroxEntities errors.
Public Overridable Property PhotoAlbums() As DbSet(Of PhotoAlbum)
Public Overridable Property Pictures() As DbSet(Of Picture)


However, when I run the NewPhotoAlbum.aspx, entered an entry and clicked Insert. I encounter another error at MyBase.New("name=PlanetWroxEntities"). The error is attached at the following link.

The error:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.

Please help.
https://s13.postimg.org/gugl5cpev/Sc...v_23_21_29.jpg

The error code on the page
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: An error occurred creating the configuration section handler for entityFramework: Could not load file or assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Source Error:


Line 7: <configSections>
Line 8: <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
Line 9: <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.Entit yFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Line 10: </configSections>

Last edited by hansheung; November 23rd, 2016 at 10:02 AM..
 
Old November 24th, 2016, 05:12 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Looks like you're mixing EF5 and EF6. You may have to fully uninstall them (including cleaning up web.config) and then install one or the other. Take a look at these threads for more info:

the book and problem with EF
Chapter 14, first try it out - Compilation Error Planet Wrox - Entity Framework

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!
 
Old November 30th, 2016, 04:19 AM
Registered User
 
Join Date: Nov 2016
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Solved, thanks Imar.

I managed to solve my errors by going to Website -> Manage NuGet. Choose Entity Framework version 6.x.x. Update or install it. Load NewPhotoAlbum, key in an entry and everything works. Thanks Imar.
 
Old November 30th, 2016, 04:33 AM
Registered User
 
Join Date: Nov 2016
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Imar, another error pop up when I try to insert the description, Tooltips and ImageUrl. Please help, I don't know where to start debugging.

https://s12.postimg.org/s4vycanmh/Sc...v_30_16_31.jpg

System.Web.HttpException was unhandled by user code
ErrorCode=-2147467259
HResult=-2147467259
Message=DataBinding: 'System.Data.Entity.DynamicProxies.Picture_E8A62F0 EC52366028B3F9AAA43609A3BF087C764352C7F4411F4BCC2C 4F487E9' does not contain a property with the name 'Description'.
Source=System.Web
WebEventCode=0
StackTrace:
at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
at System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts)
at System.Web.UI.DataBinder.Eval(Object container, String expression)
at System.Web.UI.TemplateControl.Eval(String expression)
at ASP.managephotoalbum_aspx.__DataBinding__control16 (Object sender, EventArgs e) in C:\BegASPNET\Site\ManagePhotoAlbum.aspx:line 16
at System.Web.UI.Control.OnDataBinding(EventArgs e)
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.Control.DataBindChildren()
InnerException:



Code:
<%@ Page Title="Manage Photo Album" Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="ManagePhotoAlbum.aspx.vb" Inherits="_ManagePhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" InsertItemPosition="LastItem" SelectMethod="ListView1_GetData" InsertMethod="ListView1_InsertItem" DeleteMethod="ListView1_DeleteItem">
    <InsertItemTemplate>
        <li>
            Description: <asp:TextBox ID="Description" runat="server" TextMode="MultiLine" Text='<%#Bind("Description")%>' /><br />
            ToolTip: <asp:TextBox ID="ToolTip" runat="server" Text='<%#Bind("ToolTip")%>' /><br />
            ImageUrl: <asp:TextBox ID="ImageUrl" runat="server" Text='<%#Bind("ImageUrl")%>' /><br />
            <asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName ="Insert" />
        </li>
    </InsertItemTemplate>
    <ItemTemplate>
            Description: <asp:Label ID="Description" runat="server" Text='<%#Eval("Description")%>' /><br />
            Tooltip: <asp:Label ID="ToolTip" runat="server" Text='<%#Eval("ToolTip")%>' /><br />
            ImageUrl: <asp:Label ID="ImageUrl" runat="server" Text='<%#Eval("ImageUrl")%>' /><br />
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="False" />
        </li>
    </ItemTemplate>
        <LayoutTemplate>
            <ul class ="ItemContainer">
                <li runat="server" id="itemPlaceholder"></li>
            </ul>
        </LayoutTemplate>
    </asp:ListView>
</asp:Content>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 page 517 injen BOOK: Beginning ASP.NET 4.5.1 : in C# and VB 7 September 26th, 2016 10:00 AM
help in chapter 14 please nawar youssef BOOK: Beginning PHP 5.3 3 October 27th, 2011 11:36 PM
Chap 14 - pg 517 - Item 9 (Object reference not set to an instance of an object. ) jkoyle BOOK: Beginning ASP.NET 4 : in C# and VB 5 August 25th, 2011 08:50 AM
Chapter 14 JonG BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 March 21st, 2006 10:04 PM
Chapter 14 Mike Smith BOOK: Professional C#, 2nd and 3rd Editions 2 January 4th, 2004 05:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.