This is a migrated thread and some comments may be shown as answers.

Binding a SubReport to a Different DataTable than Main Report

1 Answer 129 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Andrew asked on 02 Oct 2017, 06:48 PM

I have an "invoice" main report with a "company" subreport that I'm trying to bind to a data source and report viewer at runtime.

Here is the code I was attempting to use...

protected void Page_Load(object sender, EventArgs e)
        Report myReport = new myCompiledReports.invoice();
        ReportViewer1.ReportSource = myReport;

        DataTable dtInvoice = (DataTable)Session["dtInvoice"];     
        DataTable dtCompany = (DataTable)Session["dtCompany"];     

        myReport.DataSource = dtInvoice ;
        SubReport mySubReport = (SubReport)myReport.Items.Find("company", true)[0];
        mySubReport.Report.DataSource = dtCompany;

During the SubReport mySubReport = (SubReport)myReport.Items.Find("company", true)[0];, I'm getting an index was outside the bounds of the array error.

If I comment out the subreport code, the report appears in the ReportViewer, minus the subreport information of course.

How do I bind a subreport to a different data table than the main report and have all the information appear in the ReportViewer control?

Thanks ahead for any and all help in this matter.

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 05 Oct 2017, 08:50 AM
Hello Andrew,

You can check an update in the support ticket #1133445 that was opened on the same issue. For other community members intersted in this topic, below is the reply from th ticket:

"Index was outside the bounds of the array error occurs because no SubReport item with name 'company' was found in report's Items collection.

To set the data source for the subreport at runtime you will need to create an instance of the sub report set report's (or inner data item's) DataSource property and set the created instance as SubReport.ReportSource, for example:
SubReport mySubReport = (SubReport)myReport.Items.Find("subReport1", true)[0];
// create an instance of sub report
SubReport1 subreport = new SubReport1();
subreport.DataSource = dtCompany;
mySubReport.ReportSource = new InstanceReportSource() { ReportDocument = subreport };

Note that setting ReportSource property of Report Viewers or SubReport item to a report instance is obsolete as of Q2 2012. Our recommendation to use ReportSource objects to set the viewer's ReportSource:
Report1 report1 = new Report1();
// perform additional operations on the report object if needed         
Telerik.Reporting.InstanceReportSource instanceReportSource = new  Telerik.Reporting.InstanceReportSource();
instanceReportSource.ReportDocument = report1;
this.ReportViewer1.ReportSource = instanceReportSource;

For more detailed information please check How to migrate your project to utilize the new ReportSource objects KB article."

Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
General Discussions
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question