Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
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 14th, 2013, 09:47 AM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 14 errors for Inserting and Deleting Data with the ListView Control

Can someone help me with this error from Try It Out Inserting and Deleting Data with the ListView Control?

Error
Server Error in '/Site' Application.

Object reference not set to an instance of an object.

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 6: Protected Sub EntityDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangedE ventArgs) Handles EntityDataSource1.Inserted
Line 7: Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)
Line 8: Response.Redirect(String.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", New String(myPhotoAlbum.Id.ToString())))
Line 9: End Sub
Line 10: End Class

Source File: N:\NewASP\Site\NewPhotoAlbum.aspx.vb Line: 8




Here is my code for 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 ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="EntityDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" 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=TesterEntities" DefaultContainerName="PlanetWroxEntities" EnableFlattening="False"
EnableInsert="True" EntitySetName="PhotoAlbums" OnInserted="EntityDataSource1_Inserted">
</asp:EntityDataSource>
</asp:Content>


Code behind
Imports TesterModel

Partial Class _NewPhotoAlbum
Inherits BasePage

Protected Sub EntityDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangedE ventArgs) Handles EntityDataSource1.Inserted
Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)
Response.Redirect(String.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", New String(myPhotoAlbum.Id.ToString())))
End Sub
End Class
 
Old May 14th, 2013, 10:08 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,

Can you comment out the line with the redirect statement? EF may be throwing an error that you don't see because of the cast to null.

Also, you don't need the New String part. This should work:

Code:
Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)
Response.Redirect(String.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()))
Finally, check your database. Are you sure that both PhotoAlbum and Picture have a column called Id that is of type int and set to an Identity? Also, did you link Picture.PhotoAlbumId to PhotoAlbum.Id? If you have to (or already have) make changes to the database, don't forget to refresh or recreate the EF model before you try again.

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 14th, 2013, 10:43 AM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Hi Imar and thanks for your quick response.
However I did not understand what you meant by
"Can you comment out the line with the redirect statement? EF may be throwing an error that you don't see because of the cast to null."
Where is that line?
1. The tables Picture and PhotoAlbum both have Id set as primary key. After I removed the part you asked me to remove, I still got the following error.

2. You asked if I "did you link Picture.PhotoAlbumId to PhotoAlbum.Id?" I dont know the answer to this. How do I check to make sure I link them?


Object reference not set to an instance of an object.

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 6: Protected Sub EntityDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangedE ventArgs) Handles EntityDataSource1.Inserted
Line 7: Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)
Line 8: Response.Redirect(String.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()))
Line 9: End Sub
Line 10: End Class

Source File: N:\NewASP\Site\NewPhotoAlbum.aspx.vb Line: 8

Last edited by winkimjr2; May 14th, 2013 at 10:47 AM..
 
Old May 14th, 2013, 10:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Just comment out this line:

Code:
Response.Redirect(String.Format("ManagePhotoAlbum.  aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()))
E.g.:


Quote:
' Response.Redirect(String.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()))
To check the relationships, check out the database diagram you created earlier. Then check the relationship between these two tables (right-click the line between them and choose Properties). Then make sure they are linked on the tables and columns I mentioned earlier.

Note: doing this from memory so you may see other stuff than I am describing here. In that case, let me know and I'll look up the page numbers that show how to do this.

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 14th, 2013, 11:04 AM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Yes the tow tables are linked with Picture Id and PhotoAlbumId. I did recreate the link and refreshed the database.

I did comment on the line of code you asked me to:
'Response.Redirect(String.Format("ManagePhotoAlbum .aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()))

And now I have the following error.

Violation of PRIMARY KEY constraint 'PK_PhotoAlbum'. Cannot insert duplicate key in object 'dbo.PhotoAlbum'. The duplicate key value is (0).
The statement has been terminated.

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.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_PhotoAlbum'. Cannot insert duplicate key in object 'dbo.PhotoAlbum'. The duplicate key value is (0).
The statement has been terminated.
 
Old May 14th, 2013, 11:14 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
And now I have the following error.

Violation of PRIMARY KEY constraint 'PK_PhotoAlbum'. Cannot insert duplicate key in object 'dbo.PhotoAlbum'. The duplicate key value is (0).
Ah, there we have it. This seems to suggest that the Id column of the PhotoAlbum table is set to be the key, but not an identity. An identity auto numbers each row in the table, generating unique IDs. Without it, the Id will be zero (the default for an int in .NET) the first time you insert an album. The second time, the code will crash as you try to insert another row with the same ID of zero.

Check the section of the book that shows you how to create the PhotoAlbum and Picture tables. Make sure you set Is Identity for both Id columns to Yes. Then save the changes and refresh or recreate the EF model.

Hope this helps,

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:
winkimjr2 (May 15th, 2013)
 
Old May 14th, 2013, 05:15 PM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default

When I click insert on the ManagePhotoAlbum, I am getting the following error. I am not sure how to fix it.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "Tester", table "dbo.PhotoAlbum", column 'Id'.
The statement has been terminated.

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.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "Tester", table "dbo.PhotoAlbum", column 'Id'.
The statement has been terminated.

Last edited by winkimjr2; May 15th, 2013 at 10:40 AM..
 
Old May 15th, 2013, 02:28 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

This error usually happens for one of the following two reasons:

1. You're not passing the correct ID of the album to the ManagePicture.aspx page. This could be the case if you pass something like PhotoAlbumId in the query string, but try to retrieve it as PhotoAlbum.

2. The code that associates the picture with the album is not correct or does not execute.

For both cases, posting the full code (mark up and code behind) would help in figuring this out.

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 ListView MarshallN BOOK: Beginning ASP.NET 4 : in C# and VB 0 August 18th, 2012 08:19 PM
Chapter 14 Paging Data with the Listview and DataPager Controls rmanapul BOOK: Beginning ASP.NET 4 : in C# and VB 4 May 22nd, 2012 04:10 AM
Chapter 14 Customizing Templates of the ListView Control rmanapul BOOK: Beginning ASP.NET 4 : in C# and VB 7 May 21st, 2012 04:42 AM
Ch 14 Customizing Templates of the ListView Control leeWozyWarren BOOK: Beginning ASP.NET 4 : in C# and VB 3 December 4th, 2011 06:16 AM
Chapter 14, Listview control problem SamuelMSr BOOK: Beginning ASP.NET 4 : in C# and VB 3 September 28th, 2011 02:47 PM





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