Unable to cast object of type 'CMS.CustomTables.CustomTableItem' to type

Nattawut Comlon asked on December 8, 2015 03:30

Hi,

I have a project which is recently upgraded from version 8.2 to version 9.0. I find that on a particular api has thrown some exception.

I have created a new custom table and using code generator to create a class for that custom table. Then I call an api to retrieve the data from that custom table like this.

CustomTableItemProvider.GetItems<SampleTableItem>().TypedResult;

It works fine on version 8.2 but on upgraded version 9, it returns error like below:

Unable to cast object of type 'CMS.CustomTables.CustomTableItem' to type '{SampleTableItem-classname}'

I am not sure what causes that issue. Could you guide me where to check on that error?

Thank you so much in advance, - Nattawut

Recent Answers


Victor Cavendish-Góes answered on December 8, 2015 14:46 (last edited on December 8, 2015 19:56)

Nattawut,

Have you tried re-downloading the class from code generator in v9? Have you also attempted to not use the ".TypedResult":

var query = CustomTableItemProvider.GetItems<SampleTableItem>();

0 votesVote for this answer Mark as a Correct answer

Nattawut Comlon answered on December 9, 2015 01:35

Hi Victor,

Yes, the code is newly re-generated and I also tried to use without the .TypedResult. It is the same error message. so far.

0 votesVote for this answer Mark as a Correct answer

Suman Layek answered on February 22, 2016 19:41 (last edited on February 22, 2016 19:43)

I am using the code out of the Kentico application and getting the same error as discussed on this article. Attached is the screenshot of the same. Please let us know how to solve. Right click and open image in new tab

0 votesVote for this answer Mark as a Correct answer

Conor Breen answered on April 1, 2016 21:08

Hi,

Any update on this issue? Exact same thing happening for me. Used newly generated code file in Kentico 9, got below error (showing stack trace), but exact same code worked previously in Kentico 8.2. I've tried updating to latest hotfix (9.0.17) and regenerating code after, but there were no differences and it still isn't working...

The auto-generated code classes are an extremely useful time saving feature that we use in many projects, we really need to get this back up and running again...

Image Text

Thanks, Conor

0 votesVote for this answer Mark as a Correct answer

vasu yerramsetti answered on April 27, 2016 15:52 (last edited on April 27, 2016 16:54)

Hi, We start working on Kentico 9 MVC (9.0.0 and 9.0.20). We are getting the same error in both hot-fixes while reading the data from Custom table. Its fine reading the data from custom table into MVC solution but problem is unable to cast with below code snippet.

Reference URL for Retrieving custom table data: https://docs.kentico.com/display/K9/Retrieving+content+on+MVC+sites

Error:Unable to cast object of type 'CMS.CustomTables.CustomTableItem' to type 'CMS.CustomTables.Types.ThingsItem'

But as per my observation its working fine with DancingGoat MVC project which is shared by Kentico and DacningGoat MVC project used Kentico 9.0.14 hotfix and Kentico.Web.Mvc 1.0.0 Nuget Package.

Is it a DLL issue or BUG in KENTICO 9 MVC Release?

Please someone help me to resolve this issue ASAP.

Thanks

0 votesVote for this answer Mark as a Correct answer

vasu yerramsetti answered on April 28, 2016 10:46

Hi,

Issue is resolved from my side using below assembly attribute added into Assemblyinfo.cs under properties folder in my external MVC application.

using CMS;

[assembly: AssemblyDiscoverable] Reference URL for more details: click Here

After adding this attribute in assemblyinfor.cs, Now I am able to read data from Kentico Custom table into MVC application and render without any type cast errors.

I received this resolution from Kentico Support team. Thank youuu...Support team.

I hope this can help you

Thanks

2 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.