Unable to cast object (after upgrade to R2)


After upgrading to R2 I'm getting errors in PageList when casting from PageBase to custom PageType (created with PageTypeBuilder).

<a href="<%# ((Article)Container.DataItem).LinkURL %>">
<%# shortenIngress(((Article)Container.DataItem).Ingress, 400)%>


[InvalidCastException: Unable to cast object of type 'EPiServer.Core.PageData' to type 'Site.Web.Templates.Public.PageTypes.Article'.]
   ASP.templates_public_pages_newsroom_aspx.__DataBind__control4(Object sender, EventArgs e) in c:\projects\Site\Templates\Public\Pages\NewsRoom.aspx:25
   System.Web.UI.Control.OnDataBinding(EventArgs e) +132
The PageType is inheriting from PageData.
Any ideas in resolving the issue?
Nov 03, 2011 16:42

you, you need to use PageTypeBuilder to do this from my experience


Nov 03, 2011 21:45

Gaaah, works on some but not on others.


Thanks Joshua. Error handling...

Nov 04, 2011 14:22

Do all the pagetypes inherit from typed page data.  or is container.DataItem null.  that could be an issue since it can't cast to type Pagedata

Nov 04, 2011 15:04

Yes, they do inherit from Pagedata, through base classes.


The DataItem should not be null. It all worked before the upgrade to R2 

Nov 04, 2011 15:10

What is the error that you are recieving now

Nov 04, 2011 15:11
<EPiServer:PageList runat="server" ID="ConnectedArticles">
<div class="post">
<div class="heading">
<EPiServer:Property ID="Property3" runat="server" PropertyName="PageLink" />
<p class="timestamp"><%# ReturnPublishDate((EPiServer.Core.PageData)Container.DataItem)%> |
<%# GetCategoryName((EPiServer.Core.PageData)Container.DataItem)%></p>

<div class="mainBody ingress">
<p><%# ReturnIngress((Article)PageTypeBuilder.PageTypeResolver.Instance.ConvertToTyped((EPiServer.Core.PageData)Container.DataItem))%></p>
Actually this PageList works - no errors, but it would be better using a method converting to correct page type or using code behind instead of the page(view). 
Nov 04, 2011 15:18

Weird, i have used this on all my 6r2 projects and it works.  Is it something maybe that isn't of type article.  I haven't ran into that issue.

I wrote an extension method for passing in data item and returns the type instead of that long chain of code.  data.Get<Article>() returns article page.

Nov 04, 2011 15:23

Yepp, got some help from Joel Abrahamson and trying out with the AsTyped extension.

 .AsTyped() when getting a PageCollection solved it.
PageDataCollection pdc = DataFactory.Instance.FindPagesWithCriteria(pageLink, criterias).AsTyped();

Thank you for your help, Joshua,


Edited, Nov 04, 2011 16:01
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.