Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : 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 May 10th, 2012, 09:25 PM
Registered User
 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default chapter 14 try out page 497

Hi Imar,

When I try to view NewPhotoAlbum.aspx getting following error

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 17:
Line 18: PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Line 19: Response.Redirect(string.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()));
Line 20: }
Line 21: }

I verified my code with the downloaded code there is no difference

Also done debugging it says myPhotoAlbum is null

Dont know how to fix it.

My NewPhotoAlbum.aspx is

<%@ 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 ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="EntityDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" InsertVisible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=PlanetWroxEntities" DefaultContainerName="PlanetWroxEntities" EnableFlattening="False" EnableInsert="True" EntitySetName="PhotoAlbums" OnInserted="EntityDataSource1_Inserted">
</asp:EntityDataSource>
</asp:Content>
My NewPhotoAlbum.aspx.cs is

public partial class _NewPhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
{

PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Response.Redirect(string.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()));
}
}

Pl. help me fix the code.

Mary Sunish

Last edited by marysunish; May 10th, 2012 at 09:49 PM..
 
Old May 11th, 2012, 03:03 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,

Are you sure all Id columns in the database are primary key *and* an Identity? If not, fix them in the database and then recreate the EF model. You can find more info here: Cannot redirect to ManagePhotoAlbum (targets the 3.5 book with LINQ to SQL instead of EF but the principle is the same).

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 May 11th, 2012, 04:01 PM
Registered User
 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanksfor your reply

I copied your mdf file to mine and it works now.

Thanks

Mary Sunish





Similar Threads
Thread Thread Starter Forum Replies Last Post
EntityDataSource Try it Out Exercise Page 497 bimalk BOOK: Beginning ASP.NET 4 : in C# and VB 6 December 5th, 2011 01:27 PM
Chapter 1 page 14 kermit1965 BOOK: Professional ASP.NET MVC 2 6 October 12th, 2010 10:10 AM
Chapter 14 Page 548 Try it Out andreas5 BOOK: Beginning ASP.NET 4 : in C# and VB 7 August 22nd, 2010 07:48 AM
Chapter 14:Page 494 try it out Arya BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 March 18th, 2010 10:34 AM





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