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