p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4.5 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=710)
-   -   File Upload error / Ch. 14 (http://p2p.wrox.com/showthread.php?t=94045)

wassim84 January 30th, 2015 05:11 AM

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

Imar January 30th, 2015 07:11 AM

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

wassim84 January 30th, 2015 08:13 AM

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()));
}
}

Imar January 30th, 2015 08:47 AM

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

wassim84 February 1st, 2015 09:51 AM

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

Imar February 1st, 2015 10:13 AM

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

wassim84 February 1st, 2015 01:00 PM

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

Imar February 1st, 2015 01:17 PM

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

wassim84 February 1st, 2015 01:39 PM

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

Imar February 1st, 2015 06:08 PM

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


All times are GMT -4. The time now is 11:24 AM.

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