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 January 25th, 2018, 05:33 AM
Registered User
 
Join Date: Sep 2014
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 14 (pg 513 - 518)

I am getting the following error at step 10 when I try to test the functionality:

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.


Here is the markup:
<%@ 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='<%# Eval("Description")%>' /><br />
ToolTip: <asp:Label ID="ToolTip" runat="server" Text='<%# Eval("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>



And this is the 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 January 25th, 2018, 07:46 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Maybe you're running into what's described at the start of step 10?

Quote:
To test out the functionality you have so far, save all your changes, close all open files, and then request NewPhotoAlbum.aspx in your browser. Make sure you don’t accidentally open ManagePhotoAlbum.aspx, because it requires a query string that is sent by NewPhotoAlbum.aspx.
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:
NRM001 (January 25th, 2018)
 
Old January 25th, 2018, 09:30 AM
Registered User
 
Join Date: Sep 2014
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Ah, yes. It does work when you follow the instructions and open NewPhotoAlbum not ManagePhotoAlbum. Many thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 14 pg 502-503 Okadante BOOK: Beginning ASP.NET 4.5 : in C# and VB 1 April 18th, 2017 08:08 AM
chapter 14,Try it out in page 505 and 513 shameema BOOK: Beginning ASP.NET 4.5.1 : in C# and VB 16 March 29th, 2015 03:27 AM
Chapter 14, Try-it-out 515-518 supdike BOOK: Beginning ASP.NET 4 : in C# and VB 7 June 8th, 2012 05:26 PM
Chapter 14 Exercise 3 Solution pg 747 arodrigu12 BOOK: Beginning ASP.NET 4 : in C# and VB 2 April 21st, 2011 07:58 PM
chapter 14 Try It Out pg 505 step #4 arodrigu12 BOOK: Beginning ASP.NET 4 : in C# and VB 3 April 21st, 2011 02:28 PM





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