Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : 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 January 30th, 2015, 05:11 AM
Authorized User
Points: 73, Level: 1
Points: 73, Level: 1 Points: 73, Level: 1 Points: 73, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2015
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default File Upload error / Ch. 14

I am having a problem in Chapter 14, when I insert a new photo album then browsing for the (.jpg) file, assoon as I click insert, an error is thrown.

The error (in browser) is:
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


I have no idea what it is, I have reviewed the section in chapter 19 about IIS, but I did not know the application name to set, and I feel it is not the cause to the problem, it is something else.

Note: Everything else is working, When I opened the default page in (Photo Album) folder and selected (from the drop down) the photo album name I can find an empty square (containing no picture).

Any help :/

Last edited by wassim84; January 30th, 2015 at 06:04 AM.
Reply With Quote
  #2 (permalink)  
Old January 30th, 2015, 07:11 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,957
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Hi there,

Can you post the code for the page that uploads the image and saves it in the database? What do you see when you look in the Pictures table in the database. Does each picture have a correct URL?

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
  #3 (permalink)  
Old January 30th, 2015, 08:13 AM
Authorized User
Points: 73, Level: 1
Points: 73, Level: 1 Points: 73, Level: 1 Points: 73, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2015
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default

in the database, the picture URL is simply the path of the jpg file, like:
C:\Users\Wassim\Desktop\Philadelphia School\Academic\2014_2015\Logo_pictures\logo_1.jpg

the code of the page uploading is:
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
Picture myPicture = (Picture)e.Entity;
myPicture.PhotoAlbumId = photoAlbumId;

FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
string virtualFolder = "~/GigPics/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);

FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder, fileName + extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator cusValImage = (CustomValidator)ListView1.InsertItem.FindControl( "cusValImage");
cusValImage.IsValid = false;
e.Cancel = true;
}
}


as for the page creating the new photo album (then redirect to manage photot album) the code is as follows:

protected void EntityDataSource1_Inserted1(object sender, EntityDataSourceChangedEventArgs e)
{
if (e.Entity != null)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Response.Redirect(String.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()));
}
}
Reply With Quote
  #4 (permalink)  
Old January 30th, 2015, 08:47 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,957
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

That path doesn't look right; it should be a virtual path, not a physical path. How did the path get there? Looking at your code it seems that ImageUrl should get a virtual path assigned.

When you debug this code, what do you see in myPicture.ImageUrl? You should see a virtual path and a GUID as the filename.

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
  #5 (permalink)  
Old February 1st, 2015, 09:51 AM
Authorized User
Points: 73, Level: 1
Points: 73, Level: 1 Points: 73, Level: 1 Points: 73, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2015
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default

The image URL is not getting to the database, it is not creating a new picture.
As soon as I click insert this error is thrown at the browser:

Something like cannot reference an instance of an object....
Reply With Quote
  #6 (permalink)  
Old February 1st, 2015, 10:13 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,957
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Can you be more specific about the actual error message and when it occurs? Also, did you try debugging the app to see where it breaks exactly?

Finally, what's the exact URL in the browser's address bar when this error occurs? Is there a photo album ID in the query string?

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
  #7 (permalink)  
Old February 1st, 2015, 01:00 PM
Authorized User
Points: 73, Level: 1
Points: 73, Level: 1 Points: 73, Level: 1 Points: 73, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2015
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default

I am doing the following steps:

- Opening Newphotoalbum.aspx
- creating in the browser a new photo album (that is created instantly in the sql database)
- entering desciption then tooltip then choosing a file from my computer (like my pictures)
- When I click insert it gives me the following error:

Server Error in '/site' application
Object reference not set to an instance of an object
7

Every thing else is working properly, the connection to database the other pages linked to database and entity...
This is why I assume that there is a problem when uploading the file... But I cannot figure out what to do, I m thinking of redoing the whole code of managephotoalbum.aspx
Reply With Quote
  #8 (permalink)  
Old February 1st, 2015, 01:17 PM
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,957
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

It would help if you answered the questions I asked in my previous post:

1. Can you be more specific about the actual error message and when it occurs? (When meaning where in the code; the stack trace you should see in the browser would help.)

2. Also, did you try debugging the app to see where it breaks exactly?

3. Finally, what's the exact URL in the browser's address bar when this error occurs?

4. Is there a photo album ID in the query string?

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
  #9 (permalink)  
Old February 1st, 2015, 01:39 PM
Authorized User
Points: 73, Level: 1
Points: 73, Level: 1 Points: 73, Level: 1 Points: 73, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2015
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default

It would help if you answered the questions I asked in my previous post:

1. Can you be more specific about the actual error message and when it occurs? (When meaning where in the code; the stack trace you should see in the browser would help.)



[NullReferenceException: Object reference not set to an instance of an object.]
_ManagePhotoAlbum.ListView1_ItemInserting(Object sender, ListViewInsertEventArgs e) in c:\BegASPNET\Site\ManagePhotoAlbum.aspx.cs:33
System.Web.UI.WebControls.ListView.OnItemInserting (ListViewInsertEventArgs e) +112
System.Web.UI.WebControls.ListView.HandleInsert(Li stViewItem item, Boolean causesValidation) +230
System.Web.UI.WebControls.ListView.HandleEvent(Eve ntArgs e, Boolean causesValidation, String validationGroup) +623
System.Web.UI.WebControls.ListView.OnBubbleEvent(O bject source, EventArgs e) +227
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.ListViewItem.OnBubbleEve nt(Object source, EventArgs e) +112
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.Button.OnCommand(Command EventArgs e) +114
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +159
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724


2. Also, did you try debugging the app to see where it breaks exactly?
When I am debuging it it crashes

It crashes in the bolded line below:
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))

{
CustomValidator cusValImage = (CustomValidator)ListView1.InsertItem.FindControl( "cusValImage");
cusValImage.IsValid = false;
e.Cancel = true;
}
}


3. Finally, what's the exact URL in the browser's address bar when this error occurs?

http://localhost:10327/Site/ManagePhotoAlbum.aspx?PhotoAlbumId=1032

4. Is there a photo album ID in the query string?
I did ot understand exactly where...

Thank you for your help...
Reply With Quote
  #10 (permalink)  
Old February 1st, 2015, 06:08 PM
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,957
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Ah, maybe this is it?

FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload");

Maybe your control is not called FileUpload but FileUpload1 or something else? If that's the case, pass "FileUpload1" to the FindControl method instead.

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
The Following User Says Thank You to Imar For This Useful Post:
wassim84 (February 2nd, 2015)
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
Error in Ch. 14 Try It Out FrugalRacer BOOK: Beginning ASP.NET 4 : in C# and VB 5 September 29th, 2014 07:38 AM
Ch.14 .NETFramework error smileys6971 BOOK: Beginning ASP.NET 4 : in C# and VB 8 February 25th, 2013 02:06 PM
ch.14 - admin_transact is not correct file? student998 BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 March 18th, 2011 12:17 PM
CH 14 - Link Error - Unresolved Razzy The Pug BOOK: Ivor Horton's Beginning Visual C++ 2005 2 March 18th, 2008 07:44 PM
ch.14 file errors caseyc BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 10 February 28th, 2006 03:59 PM



All times are GMT -4. The time now is 10:50 AM.


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