Winforms - Can't cast UriReportSource to InstanceReportSource

1 Answer 8 Views
Report Viewer - WinForms
Richard
Top achievements
Rank 1
Iron
Iron
Richard asked on 15 Jul 2025, 02:21 PM

I need to get the values of some fields shown on a report in a VB.NET / Winforms app, and I've used the code from here;

https://docs.telerik.com/reporting/embedding-reports/program-the-report-definition/access-report-items-programmatically

However, I'm experiencing a crash at the first step of getting the InstanceReportSource with a cast exception as per the title.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim instanceReportSource As InstanceReportSource =
                DirectCast(Me.TKReportViewer.ReportSource, InstanceReportSource) <========= Invalid Cast here
    Dim report As Telerik.Reporting.Report =
                DirectCast(instanceReportSource.ReportDocument, Telerik.Reporting.Report)

    Dim txt As Telerik.Reporting.TextBox =
                TryCast(report.Items.Find("quoteReferenceNumber", True)(0), Telerik.Reporting.TextBox)

End Sub

I can't find any other questions / answers on this - anyone have an idea what might be wrong?

1 Answer, 1 is accepted

Sort by
0
Ivet
Telerik team
answered on 18 Jul 2025, 10:50 AM

Hello Richard,

Thank you for reaching out.

The cast exception occurs because the ReportSource property of your TKReportViewer is not an InstanceReportSource, but most likely a UriReportSource or TypeReportSource. This is typical when loading reports created with the Standalone Report Designer, as those are usually loaded from a file or by type, not directly as an instance.

You can only access the report's items directly when you have a report instance in code, i.e., when using InstanceReportSource with a report object you created in code. When using UriReportSource or TypeReportSource, the report is loaded and processed internally, and you do not have direct access to its items at runtime.

If you must use a .trdp/.trdx file (from the Standalone Designer), you cannot access the report items directly from the viewer. For reading values, consider exporting the report to a document format (PDF, Excel, etc.) and reading the values from there, or redesign your workflow to use code-based reports.

If you need the "Report" object, you could unpackage the TRDP file by using the Uri property of the viewer's ReportSource, for example:

Dim UriReportSource As uriReportSource = DirectCast(Me.TKReportViewer.ReportSource, Telerik.Reporting.UriReportSource)
Dim reportPackager = New ReportPackager()
Using sourcetStream = System.IO.File.OpenRead(uriReportSource.Uri)
    Dim report = reportPackager.Unpackage(sourcetStream)
End Using

Please test it and let me know if further questions arise.

Regards,
Ivet
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Report Viewer - WinForms
Asked by
Richard
Top achievements
Rank 1
Iron
Iron
Answers by
Ivet
Telerik team
Share this question
or