Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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 December 26th, 2010, 08:00 PM
Authorized User
 
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
Default 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
 
Old December 27th, 2010, 10:32 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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:
ksnortum (December 28th, 2010)
 
Old December 28th, 2010, 10:44 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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:
ksnortum (December 28th, 2010)
 
Old December 28th, 2010, 05:12 PM
Authorized User
 
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
Default Getting IDs correct

I need to spell those IDs correctly. It's my spelling problems again.

Thanks for the help.
 
Old December 28th, 2010, 06:24 PM
Authorized User
 
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
Default 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");
 
Old December 28th, 2010, 06:38 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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.
 
Old December 28th, 2010, 07:01 PM
Authorized User
 
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
Default

It's the same environment as before. I just fixed the typo that imar pointed out and run the thing again
 
Old December 28th, 2010, 07:12 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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.
 
Old December 29th, 2010, 06:48 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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 January 4th, 2011, 08:56 PM
Authorized User
 
Join Date: Dec 2010
Posts: 22
Thanks: 10
Thanked 0 Times in 0 Posts
Default 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?




Similar Threads
Thread Thread Starter Forum Replies Last Post
Object reference not set to an instance of an object. dotnet_user C# 2008 aka C# 3.0 2 April 6th, 2011 06:11 AM
Chap 13 pg448: Object reference not set to an instance of an object tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 15 August 8th, 2009 06:14 AM
Object variable not set or With Block not set brucechess BOOK: Beginning VB.NET Databases 1 March 3rd, 2009 07:30 AM
Chapter 14 TIO #1406 Object That Provides a DataSe dcollins BOOK: Beginning ASP.NET 2.0 and Databases 0 October 23rd, 2006 01:35 PM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM





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