'Unable to cast object of type 'MyAssembly.MyReport' to type 'Telerik.Reporting.ReportSource'.'

1 Answer 190 Views
Report Viewer - ASP.NET
Walter
Top achievements
Rank 1
Walter asked on 20 Oct 2023, 07:40 PM

I am using Telerik Reporting 12.1.18.620 in my ASP.NET (4.8) web application. It has been working fine for years but I suddenly encountered this error.

    public void SetReport()
    {
        if (hidReportName.Value.Length > 0)
        {
            var reportPlaintext = hidReportName.Value.QsDecrypt();
            var report = string.Format("TelerikReports.{0}, TelerikReports, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", reportPlaintext);
            try
            {
                Type reportType = Type.GetType(report);
                IReportDocument reportDocument = (IReportDocument)Activator.CreateInstance(reportType);
                Report = reportDocument as Report;
                ReportViewer.ReportSource = (ReportSource)reportDocument;  //Here is where the error occurs
            }
            catch (Exception)
            {
                throw new Exception(String.Format("Error loading Telerik report '{0}'", ReportName.QsDecrypt()));
            }
        }

I have searched extensively and tried to resolved this error but I am stymied. This is a huge blocker because I can't deploy the latest (non-reporting) code to production until this is fixed. I have been working on this for a week now with no luck.

Please help!

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 25 Oct 2023, 08:22 AM

Hi Walter,

The ReportSource property of the report viewer expects an object that inherits from the Telerik.Reporting.ReportSource base class and the error is because you are giving it a Telerik.Reporting.Report instance instead.

To resolve the problem, you may wrap your report instance in an InstanceReportSource and then pass it to the viewer, for example:

                Type reportType = Type.GetType(report);
                IReportDocument reportDocument = (IReportDocument)Activator.CreateInstance(reportType);
                Report = reportDocument as Report;
                ReportViewer.ReportSource = new InstanceReportSource() { ReportDocument = Report };

For more details on this topic, please refer to the Setting ReportSource to ReportViewers Explained - Telerik Reporting article.

I hope this helps.

Regards,
Dimitar
Progress Telerik

Stay tuned by visiting our roadmap and feedback portal pages, enjoy a smooth take-off with our Getting Started resources, or visit the free self-paced technical training at https://learn.telerik.com/.
Walter
Top achievements
Rank 1
commented on 26 Oct 2023, 05:03 PM

Using InstanceReportSource solved my issue. Thanks!
Tags
Report Viewer - ASP.NET
Asked by
Walter
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or