Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
Old December 27th, 2010, 10:32 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
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.
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
ksnortum (December 28th, 2010)
  #3 (permalink)  
Old December 28th, 2010, 10:44 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
ksnortum (December 28th, 2010)
  #4 (permalink)  
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.
Reply With Quote
  #5 (permalink)  
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");
Reply With Quote
  #6 (permalink)  
Old December 28th, 2010, 06:38 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
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.
Reply With Quote
  #7 (permalink)  
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
Reply With Quote
  #8 (permalink)  
Old December 28th, 2010, 07:12 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
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.
Reply With Quote
  #9 (permalink)  
Old December 29th, 2010, 06:48 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 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!
Reply With Quote
  #10 (permalink)  
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?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 07: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 07: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 02: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



All times are GMT -4. The time now is 03:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.