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 Unable to cast object of type exception (http://p2p.wrox.com/showthread.php?t=99294)

nkiuk December 11th, 2016 06:03 AM

Chapter 14 Unable to cast object of type exception
 
Hi,

below is my code
Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;



public partial class NewPhotoAlbum : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

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

I get a "Invalid Cast Exception was unhandled by user code" error
i.e
{"Unable to cast object of type 'PlanetWroxModel.PhotoAlbum' to type 'PhotoAlbum'."}
At:
Code:

PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Could someone please explain why and how I can fix this issue.

Cheers.

Imar December 12th, 2016 11:06 AM

Sounds like yo have another class called PhotoAlbum somewhere. Do you have a page called PhotoAlbum maybe or another class in App_Code?

Imar

nkiuk December 14th, 2016 08:00 PM

Quote:

Originally Posted by Imar (Post 308453)
Sounds like yo have another class called PhotoAlbum somewhere. Do you have a page called PhotoAlbum maybe or another class in App_Code?

Imar

Hi Imar thanks for your reply.

No I do not have another class or page called PhotoAlbum somewhere else.

When I click 'Insert' the underhanded exception error occurs but the data gets inserted in the Database.

I Also get this message:
An exception of type 'System.InvalidCastException' occurred in App_Web_y5wab1hk.dll but was not handled in user code

Additional information: Unable to cast object of type 'PlanetWroxModel.PhotoAlbum' to type 'PhotoAlbum'

Noel

Imar December 15th, 2016 09:00 AM

>> No I do not have another class or page called PhotoAlbum somewhere else.

To see if that's really true, click on the first PhotoAlbum in this line:

PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;

and then press F12. Where does it take you?

trurangimplant July 28th, 2018 03:06 AM

Full
 
Quote:

Originally Posted by nkiuk (Post 308449)
Hi,

below is my code
Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;



public partial class NewPhotoAlbum : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

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

I get a "Invalid Cast Exception was unhandled by user code" error
i.e
{"Unable to cast object of type 'PlanetWroxModel.PhotoAlbum' to type 'PhotoAlbum'."}
At:
Code:

PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Could someone please explain why and how I can fix this issue.

Cheers.

Can you show me full of code


All times are GMT -4. The time now is 08:28 PM.

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