p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Chapter 14 problem (http://p2p.wrox.com/showthread.php?t=86483)

Nick Makin January 28th, 2012 10:10 AM

Chapter 14 problem
 
Hi, im currently working through chapter 14 and im on step 16 on the exercise on 548. I have added a new photo album to my site using the NewPhotoAlbum.aspx and now ManagePhotoAlbum.aspx opens, whenver i try and enter any text into this page and press "Insert" an error gets thrown...

The title of the error is "Object reference not set to an instance of an object"
and there is a line of code highlighed in my ManagePhotoAlbum.cs file...
Code:

myPicture.PhotoAlbumId = photoAlbumId
Not sure what the error means, checked my code and I cant find any silly errors in it so not sure what the problem is.

Any help would be appreciated,

Nick

Imar January 28th, 2012 03:18 PM

Hi there,

This typically happens when the ID of the new album is not correctly sent to the Manage page.
Can you post the code for the New and Manage pages? If you post code here, please copy it to Notepad first to remove color coding and then use code tags (the # on the toolbar for the forum post editor) to wrap the code.

Cheers,

Imar

Imar January 28th, 2012 03:24 PM

Hi there,

This usually means that the ID of the new album is not passed correctly to the Manage page. Can you post the code for the New and Manage pages? If you post code, please copy it to Notepad first to remove color coding and then wrap it in Code tags (using the # button on the toolbar).

Cheers,

Imar

Nick Makin January 29th, 2012 11:03 AM

Hi Imar, thanks again for the response.

Here is the code for NewPhotoAlbum.cs
Code:

protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
    {
        PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
        Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
                        myPhotoAlbum.Id.ToString()));
    }

and the ManagePhotoAlbum.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;

        FileUpload FileUpload1 =
                        (FileUpload)ListView1.InsertItem.FindControl("FileUplaod1");
        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(physicalFolder, fileName + extension));
        myPicture.ImageUrl = virtualFolder + fileName + extension;
    }
    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        FileUpload FileUpload1 =
            (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
        {
            CustomValidator cusValImage =
                (CustomValidator)ListView1.InsertItem.FindControl("cusValImage");
            cusValImage.IsValid = false;
            e.Cancel = true;
        }

Sorry for the late reply,

Nick

Imar January 29th, 2012 11:17 AM

Hi there,

A few things to try:

1. (FileUpload)ListView1.InsertItem.FindControl("File Uplaod1");

Is the control called FileUplaod1? Or FileUpload1?

2. Try debugging and look at e.Exception inside the EntityDataSource1_Inserting method. Maybe another error has occurred?

3. If this doesn't help, please post the full code including the markup of these pages so I can try it out myself.

Cheers,

Imar

Nick Makin January 31st, 2012 01:55 PM

Hi Imar, thanks for your reply and apologies for my late one.

I have made the spelling correction to my code and ran it, entered a new album and pressed the button. Then it throws the error: "Object reference not set to an instance of an object" for the code:
Code:

Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
Let me know if you want to post the markup to these 2 pages,

Thanks Imar,

Nick

Imar January 31st, 2012 02:00 PM

Did you try to debug and look at e.Exception?

Imar

Nick Makin February 13th, 2012 09:03 AM

Sorry for your really late reply, I have tried to debug the EntityDataSource1_Inserting method in the ManagePhotoAlbum.cs and I got this error straight away in my browser:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

Thanks,

Nick

Imar February 13th, 2012 09:11 AM

I figured it would be something like that. Looks like EF can't access your database. Did you make any changes to your connection string for EF and/or SQL Server?

Imar

Nick Makin February 13th, 2012 09:32 AM

No i dont think so, ive just followed the steps in the book. Is it worth going back and re-doing the connection strings?


All times are GMT -4. The time now is 04:39 AM.

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