Binding a SubReport to a Different DataTable than Main Report

2 posts, 0 answers
  1. Andrew
    Andrew avatar
    1 posts
    Member since:
    Oct 2017

    Posted 02 Oct Link to this post

    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.

  2. Katia
    Admin
    Katia avatar
    574 posts

    Posted 05 Oct Link to this post

    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."


    Regards,
    Katia
    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
Back to Top