I am working on part"Try It Out Inserting and Deleting Data with the ListView Control",page 513-518.
I am following the instruction at step 10, close all open files, and then request "NewPhotoAlbum.aspx", but when enter the description , the tooltip and select URL for image in "ManagePhotoAlbum.aspx", I get an error
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 my code for "NewPhotoAlbum.aspx":
Code:
<%@ Page Title="Create New Photo Album" Language="C#" MasterPageFile="~/MasterPages/Frontend.master"
AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" 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>
code for "NewPhotoAlbum.aspx.cs":
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _NewPhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void DetailsView1_InsertItem()
{
PhotoAlbum photoAlbum = new PhotoAlbum();
TryUpdateModel(photoAlbum);
if (ModelState.IsValid)
{
using (var myEntities = new PlanetWroxEntities2())
{
myEntities.PhotoAlbums.Add(photoAlbum);
myEntities.SaveChanges();
}
Response.Redirect(string.Format("ManagePhotoAlbum?PhotoAlbumId={0}", photoAlbum.Id.ToString()));
}
}
}
code for "ManagePhotoAlbum.aspx"
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true"
CodeFile="ManagePhotoAlbum.aspx.cs" 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>
code for "ManagePhotoAlbum.aspx.cs"
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.ModelBinding;
public partial class ManagePhotoAlbum : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public IQueryable ListView1_GetData([QueryString("PhotoAlbumId")] int photoAlbumId)
{
var myEntities = new PlanetWroxEntities2();
return from p in myEntities.Pictures
where p.PhotoAlbumId == photoAlbumId
select p;
}
public void ListView1_InsertItem([QueryString("PhotoAlbumId")] int photoAlbumId)
{
Picture picture = new Picture();
TryUpdateModel(picture);
if (ModelState.IsValid)
{
// Save changes here
using (var myEntities = new PlanetWroxEntities2())
{
picture.PhotoAlbumId = photoAlbumId;
myEntities.Pictures.Add(picture);
myEntities.SaveChanges();
}
}
}
public void ListView1_DeleteItem(int id)
{
using (var myEntities = new PlanetWroxEntities2())
{
var picture = (from p in myEntities.Pictures
where p.Id == id
select p).Single();
myEntities.Pictures.Remove(picture);
myEntities.SaveChanges();
}
}
}
I am using VS2017 Community. Can anyone help me? Thank you!