 |
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
|
|
|
|
|

December 26th, 2010, 08:00 PM
|
|
Authorized User
|
|
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Chapter 14: Object not set
Merry Christmas!
Some code from chapter 14 isn't working. The object FileUpload1 isn't set in the ItemInserting event.
Here's the error message and attached in the environment in a zip file:
https://docs.google.com/leaf?id=0B_a...N2E1MDI2&hl=en
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 34: FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
Line 35:
Line 36: if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
Line 37: {
Line 38: CustomValidator cusValImage = (CustomValidator)ListView1.InsertItem.FindControl( "cusValImage");
Source File: c:\BegASPNET\Site\ManagePhotoAlbum.aspx.cs Line: 36
|
|

December 27th, 2010, 10:32 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
This seems REALLY suspicious:
Code:
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
Visual Studio doesn't create controls that have spaces in their IDs. You have a space between "File" and "Upload1" that I'd be willing to wager a quarter on should not be there.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
|
|

December 28th, 2010, 10:44 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
I think the space is caused by the lousy way this forum handles code ;-(
However, the problem indeed seems to be with the control's name. In Code Behind you look for FileUpload1, but in the markup you called it FileUpdate1 instead.
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

December 28th, 2010, 05:12 PM
|
|
Authorized User
|
|
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Getting IDs correct
I need to spell those IDs correctly. It's my spelling problems again.
Thanks for the help.
|
|

December 28th, 2010, 06:24 PM
|
|
Authorized User
|
|
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
New problem; same error
I have the same error message but now on a different line. The object is a database object and the spelling seems to be fine. It's the Picture table coming from the "e" variable in the Inserting event. Here's the error-- the environment is the same except for the change Imar suggested:
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:
Code:
Line 18: int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
Line 19: Picture myPicture = (Picture)e.Entity;
Line 20: myPicture.PhotoAlbumId = photoAlbumId;
Line 21:
Line 22: FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
|
|

December 28th, 2010, 06:38 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Not enough context to even attempt to guess at it.
What *IS* variable e? Have you debugged to make sure it is what you think it is??? Put a breakpoint on line 20. Check that value of e. Check the value of e.Entity. Check the value of myPicture. The debugger is your best friend.
|
|

December 28th, 2010, 07:01 PM
|
|
Authorized User
|
|
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
It's the same environment as before. I just fixed the typo that imar pointed out and run the thing again
|
|

December 28th, 2010, 07:12 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Sorry, I don't have the time to download and try all that. Maybe this weekend if nobody else steps in first.
But I say again: Try using the debugger.
|
|

December 29th, 2010, 06:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Try Old Pedant's suggestion by using the debugger (see Chapter 18) and then look at e.Exception. There's a fair chance something went wrong at the database level.
If e.Exception is not null, can you post the message associated with the exception?
Cheers,
Imar
|
|

January 4th, 2011, 08:56 PM
|
|
Authorized User
|
|
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Continue the problem with debugging
I'm confused. When I start the debugging on the NewPhotoAlbum.aspx -> ManagePhotoAlbum.aspx from Chapter 14, I wind up in a place I didn't expect. The page is PlanetWrox.Designer.cs. It seems to be code that's inserted by ASP or ADO.
Code:
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String ImageUrl
{
get
{
return _ImageUrl;
}
set
{
OnImageUrlChanging(value);
ReportPropertyChanging("ImageUrl");
_ImageUrl = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("ImageUrl");
OnImageUrlChanged();
}
}
It's _ImageUrl = StructuralObject.SetValidValue(value, false); that it's stopping on, saying that value is null.
But if I step out of the getter, I see this from ManagePhotoAlbums.aspc.cs:
Code:
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
Picture myPicture = (Picture)e.Entity;
myPicture.PhotoAlbumId = photoAlbumId;
The "Arrow" is pointing at the open brace at the top. ImageUrl isn't set until the bottom of this function.
What's going on?
|
|
 |
|