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 September 14th, 2016, 01:07 PM
Registered User
 
Join Date: Sep 2016
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 14 page 517

Hello there I've been working on chapter 14 page 517 and when I request the newphotoalbum.aspx in the browser I get this error. I've compared everything with the source code that I downloaded from the site and everything seems to match up.

Server Error in '/' Application.

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

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.Data.Entity.Validation.DbEntityValidationEx ception: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Source Error:


Line 8: Using myEntities As New PlanetWroxEntities()
Line 9: myEntities.PhotoAlbums.Add(photoAlbum)
Line 10: myEntities.SaveChanges()
Line 11: End Using
Line 12: Response.Redirect(String.Format("ManagePhotoAlbum? PhotoAlbumId={0}", photoAlbum.Id.ToString()))



Also when I request the managephotoalbum.aspx I get this error, I did check to make sure my build was on 4.5.1, Thanks any help would be greatly appreciated!


Server Error in '/' Application.

A null value for parameter 'PhotoAlbumId' of non-nullable type 'System.Int32' for method 'System.Linq.IQueryable ListView1_GetData(Int32)' in '_ManagePhotoAlbum'. An optional parameter must be a reference type or a nullable type.

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.InvalidOperationException: A null value for parameter 'PhotoAlbumId' of non-nullable type 'System.Int32' for method 'System.Linq.IQueryable ListView1_GetData(Int32)' in '_ManagePhotoAlbum'. An optional parameter must be a reference type or a nullable type.
 
Old September 17th, 2016, 05:47 PM
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,

How did you define your PhotoAlbum class? Did you make fields required that you are not supplying a value for when creating a new album? Can you provide more info?

For the Manage Album page, you can't browse to the page directly. It expects the PhotoAlbumId so it knows which album to link the pictures to.

Hope this helps,

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 September 18th, 2016, 09:39 AM
Registered User
 
Join Date: Sep 2016
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey Imar, I've just been fallowing the book, but must have something set wrong somewhere. Thanks for the help.

The code for newphotoalbum.aspx

<%@ Page Title="Create New Photo Album" Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="NewPhotoAlbum.aspx.vb" Inherits="_NewPhotoAlbum" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" runat="Server">
<asp:DetailsView AutoGenerateRows="false" ID="DetailsView1" DefaultMode="Insert" runat="server" InsertMethod="DetailsView1_InsertItem">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:CommandField ShowInsertButton="True" ShowCancelButton="false" />
</Fields>
</asp:DetailsView>

</asp:Content>

The code behind newphotoalbum.aspx.vb

Partial Class _NewPhotoAlbum
Inherits BasePage

Public Sub DetailsView1_InsertItem()
Dim photoAlbum As New PhotoAlbum()
TryUpdateModel(photoAlbum)
If ModelState.IsValid Then
Using myEntities As New PlanetWroxEntities()
myEntities.PhotoAlbums.Add(photoAlbum)
myEntities.SaveChanges()
End Using
Response.Redirect(String.Format("ManagePhotoAlbum? PhotoAlbumId={0}", photoAlbum.Id.ToString()))
End If
End Sub
End Class
 
Old September 18th, 2016, 12:00 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

But how does the PhotoAlbum table look (which determines the PhotoAlbum class)? Did you set Id to be an entity? Did you add any other columns that are required?

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 September 19th, 2016, 12:47 PM
Registered User
 
Join Date: Sep 2016
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's the code I have for that. It all appears to be the same code provided from the source code, Thanks

Managephotoalbum.aspx

<%@ 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" ItemType="Picture">
<InsertItemTemplate>
<li>Description:
<asp:RequiredFieldValidator ID="reqDesc" ControlToValidate="Description" runat="server" ErrorMessage="Enter a description." />
<asp:TextBox ID="Description" runat="server" TextMode="MultiLine" Text='<%# BindItem.Description %>' /><br />
ToolTip:
<asp:RequiredFieldValidator ID="reqToolTip" ControlToValidate="ToolTip" runat="server" ErrorMessage="Enter a tool tip." />
<asp:TextBox ID="ToolTip" runat="server" Text='<%# BindItem.ToolTip %>' /><br />
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:CustomValidator ID="cusValImage" runat="server" ErrorMessage="Select a valid .jpg file." />
<asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName="Insert" />
</li>
</InsertItemTemplate>
<ItemTemplate>
<li>Description:
<asp:Label ID="Description" runat="server" Text='<%# Item.Description %>' /><br />
ToolTip:
<asp:Label ID="ToolTip" runat="server" Text='<%# Item.ToolTip %>' /><br />
<asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Item.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" />
</ul>
</LayoutTemplate>
</asp:ListView>
</asp:Content>


managephotalbum.aspx.vb

Imports System.Web.ModelBinding

Partial Class _ManagePhotoAlbum
Inherits BasePage

' The return type can be changed to IEnumerable, however to support
' paging and sorting, the following parameters must be added:
' ByVal maximumRows as Integer
' ByVal startRowIndex as Integer
' ByRef totalRowCount as Integer
' ByVal sortByExpression as String

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)
Dim FileUpload1 As FileUpload = CType(ListView1.InsertItem.FindControl("FileUpload 1"), FileUpload)
If Not FileUpload1.HasFile OrElse Not FileUpload1.FileName.ToLower().EndsWith(".jpg") Then
Dim cusValImage As CustomValidator = CType(ListView1.InsertItem.FindControl("cusValImag e"), CustomValidator)
cusValImage.IsValid = False
ModelState.AddModelError("Invalid", cusValImage.ErrorMessage)
End If
If ModelState.IsValid AndAlso Page.IsValid Then
Using myEntities As New PlanetWroxEntities()
picture.PhotoAlbumId = photoAlbumId

Dim virtualFolder As String = "~/GigPics/"
Dim physicalFolder As String = Server.MapPath(virtualFolder)
Dim fileName As String = Guid.NewGuid().ToString()
Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder, fileName + extension))
picture.ImageUrl = virtualFolder + fileName + extension

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)
Using myEntities As New PlanetWroxEntities()
Dim picture = (From p In myEntities.Pictures
Where p.Id = id
Select p).Single()
myEntities.Pictures.Remove(picture)
myEntities.SaveChanges()
End Using
End Sub
End Class
 
Old September 19th, 2016, 02:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I was talking about the *database* table. Can you look at its definition in the database and make sure Id is set to be the primary key and set as an identity?

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 September 26th, 2016, 09:13 AM
Registered User
 
Join Date: Sep 2016
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
I was talking about the *database* table. Can you look at its definition in the database and make sure Id is set to be the primary key and set as an identity?

Imar
Sorry for the late reply, I did check the photo album table and it's id is set as the primary key and it's set as an identity.

Thanks
 
Old September 26th, 2016, 10:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I pasted your code into a copy of the site ad fixed a few errors. Not sure if they are in your actual code, or are caused by an issue when pasting code here at the Wrox forum. If you paste code, please paste in Notepad first to clean up color coding and so on. Then paste in the editor and wrap the code in [code] tags using the # button on the toolbar. That should preserve the code correctly.

I made the following changes:

1. Removed a space between ManagePhotoAlbum? and PhotoAlbumId. That is: this:

("ManagePhotoAlbum? PhotoAlbumId={0}",

became this:

("ManagePhotoAlbum?PhotoAlbumId={0}",

2. Removed a space between FileUpload and 1 in the code that finds the upload control in the Isnert template.

3. Same thing for cusValImag e versus cusValImage

Again, this may be due to the forum, and may not be present in your code.

With these changes, your code works fine against my database.

If the same code doesn't work for you, it must be something with your database. Can you post the full schema for the two tables? I.e. list all columns, their data type and whether they are required or not. Also, have you made changes to the table that are not in the EF model? If so, you'll need to refresh the EF model.

If it keeps failing, create a copy of the site, delete all stuff that's not relevant to reproducing the problem, upload it somewhere and I'll take a look.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 page 508 khurramzaheer BOOK: Beginning ASP.NET 4.5.1 : in C# and VB 3 January 5th, 2015 07:39 AM
chapter 14 try out page 497 marysunish BOOK: Beginning ASP.NET 4 : in C# and VB 2 May 11th, 2012 04:01 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 1 page 14 kermit1965 BOOK: Professional ASP.NET MVC 2 6 October 12th, 2010 10:10 AM
Chapter 14:Page 481 try it out lstew452 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 June 20th, 2010 02:35 PM





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