setting datasource of subreport and NeedDataSource event problem

11 posts, 2 answers
  1. Lior ef
    Lior ef avatar
    49 posts
    Member since:
    Jan 2010

    Posted 07 Jul 2010 Link to this post

    hi guys,

    i have a master detail report that uses a certain table from a DataSet as DataSource,
    i've set:
    MasterReport.DataSource = DataSet1.Table1 and 
    MasterReport.DataMember = "GetEnumerator".
    until now, it works fine.

    now, i have a subreport in the MasterReport and i want to set its' datasource,
    because DataSet1 hold 2 tables, and Table1 is the DataSource of the MasterReport,
    i want to take the same DataSet1 and get Table2 from it to put as DataSource of the subreport,
    something like that:
    ((MasterReport.DataSource as DataSet1.Table1).DataSet.Tables["Table2"]

    of course this doesn't work because MasterReport.DataSourc is ObjectDataSource type.
    how can i do want i need in a better way?
    i want the same instance of the Dataset to be the datasource of the two reports but the datamember will be two different tables.
    i've attached a screenshot of the way my MasterReport is built.

    second problem i have is that i catched in MasterReport this event subReport1_NeedDataSource
    (definition of subReport1: "private SubReport subReport1;") and this event MasterReport_NeedDataSource,
    i've put a breakpoint in them and it never triggers. any suggestions why that happens?

    thanks in advance.
    lior.
  2. Answer
    Patrick
    Patrick avatar
    52 posts
    Member since:
    Jun 2010

    Posted 07 Jul 2010 Link to this post

    Regarding your 2nd problem... if the subreport, or the master report for that matter, has a data source already bound to it, the corresponding NeedDataSource() event will not fire. At least that's been my experience.

    What I've been doing is releasing the .DataSource property at runtime when the GUI creates an instance of the report. Here's some code... hope it helps.

        public BudgetComparativeMasterReport(string fy_code, string fy_period, string rollup_section_ind, string dept_code, string comparative_report_type)  
        {  
          /// <summary>  
          /// Required for Telerik Reporting designer support  
          /// </summary>  
          InitializeComponent();  
     
          //  
          // TODO: Add any constructor code after InitializeComponent call  
          //  
     
          try 
          {  
            // place the values passed as arguments in the report parameters  
            this.ReportParameters["fy_code"].Value = fy_code;  
            this.ReportParameters["fy_period"].Value = fy_period;  
            this.ReportParameters["rollup_section_ind"].Value = rollup_section_ind;  
            this.ReportParameters["dept_code"].Value = dept_code;  
            this.ReportParameters["fiscal_period_ending"].Value = ReportUtilities.GetPeriodEndingDate(fy_code, fy_period);  
            this.ReportParameters["comparativeReportType"].Value = comparative_report_type;  
     
            // execution of the following line will force the BudgetComparativeMasterReport_NeedDataSource event to be raised  
            this.DataSource = null;  
     
            // when the .DataSource property is set to null, we need to wire-up the NeedDataSource event handler  
            // however, the event will *not* be raised if the report already has a data source bound to it  
            this.NeedDataSource += new EventHandler(BudgetComparativeMasterReport_NeedDataSource);  
          }  
          catch (System.Exception ex)  
          {  
            // An error has occurred while filling the data set. Please check the exception for more information.  
            System.Diagnostics.Debug.WriteLine(ex.Message);  
          }  
     
        }  
     
        /// <summary>  
        /// This event is fired at the start of report processing, but only if the report has no data source bound to it!   
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void BudgetComparativeMasterReport_NeedDataSource(object sender, EventArgs e)  
        {  
          try 
          {  
            if (this.dataSet == null)  
            {  
              // collect the report parameter values assigned in the class constructor, the values  
              // will be substituted in the SQL statement  
              string fyCode = (sender as Telerik.Reporting.Processing.Report).Parameters["fy_code"].ToString();  
              string rollupSectionInd = (sender as Telerik.Reporting.Processing.Report).Parameters["rollup_section_ind"].ToString().ToUpper();  
              string deptCode = (sender as Telerik.Reporting.Processing.Report).Parameters["dept_code"].ToString();  
     
              string connectionString = ETLReportsLibrary.Properties.Settings.Default.oracleConnectionString.ToString();  
              string sqlSelect =  
                @"SELECT
                    DISTINCT dept_code,
                    dept_name,
                    fy_code,
                    rollup_section_ind
                  FROM
                    budget_comparative_report_view
                  WHERE
                    fy_code = '{0}' AND
                    Upper(rollup_section_ind) = '{1}' AND
                    dept_code LIKE '{2}'";  
     
              OracleDataAdapter da = new OracleDataAdapter(string.Format(sqlSelect, fyCode, rollupSectionInd, deptCode), connectionString);  
              this.dataSet = new DataSet();  
              da.Fill(this.dataSet);  
            }  
            (sender as Telerik.Reporting.Processing.Report).DataSource = this.dataSet;  
          }  
          catch (Exception ex)  
          {  
            System.Diagnostics.Debug.WriteLine(ex.Message);  
          }  
     
        }  
     
  3. DevCraft banner
  4. Lior ef
    Lior ef avatar
    49 posts
    Member since:
    Jan 2010

    Posted 08 Jul 2010 Link to this post

    hi Patrick,

    thanks a lot, it works, the event is triggered.

    now i'll wait for my first question to be answered :)

  5. Answer
    Peter
    Admin
    Peter avatar
    1611 posts

    Posted 12 Jul 2010 Link to this post

    Hello Lior ef,

    In order to use the Dataset's Table2 to set the SubReport's DataSource you have to create a new ObjectDataSource component with DataMember: Table2.

    For code snippets check out the How to: Bind to a DataSet help article.

    Kind regards,
    Peter
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  6. Lior ef
    Lior ef avatar
    49 posts
    Member since:
    Jan 2010

    Posted 14 Jul 2010 Link to this post

    peter, thanks for the guidance, i found the answer, i'm writing it here for other people to use:

    problem: setting the datasource of a subreport at runtime, from inside the master report using NeedData event, master reports' datasource holds a dataset we want to use.

    answer:
    first make sure to put this code at the c'tor of the master report so the event will trigger
    subReport1.ReportSource.DataSource = null;
    subReport1.NeedDataSource += new EventHandler(subReport1_NeedDataSource);

    second:
    private void subReport1_NeedDataSource(object sender, EventArgs e)
    {
        var objectDataSource = new ObjectDataSource();
        objectDataSource.DataSource = this.DataSource; // this => Master Report
        objectDataSource.DataMember = "TableName";
     
        subReport1.ReportSource.DataSource = objectDataSource;
    }
  7. Bhushan
    Bhushan avatar
    4 posts
    Member since:
    Sep 2012

    Posted 25 Sep 2012 Link to this post

    Hi Lior ef ,

    Can you plz tell me where put the following code ,what is  c'tor of the master report .not understand that.

    subReport1.ReportSource.DataSource = null;
    subReport1.NeedDataSource += new EventHandler(subReport1_NeedDataSource);

    thanks in advance.
    Bhushan.





  8. Massimiliano Bassili
    Massimiliano Bassili avatar
    323 posts
    Member since:
    May 2006

    Posted 25 Sep 2012 Link to this post

    c'tor = constructor, referring to the report constructor.
  9. Rob
    Rob avatar
    59 posts
    Member since:
    Jan 2012

    Posted 28 Aug 2013 Link to this post

    This is a very old thread, but it's exactly what I need to achieve.

    My reports run within a MVC web application that is deployed to customers sites.

    The customer has a setup web page which allows them to set select the database and servers to report off, which means I don't want to save connection strings in config files.

    I have everything working ( passing the connection string to the report from the View) , however I am struggling to pass the connection string to the sub reports

    This solution looked perfect, except it seems that the versions have changed now ,

     'subReport1.ReportSource.DataSource' no longer exists.

    Can anyone suggest a way to either access the sub reports Datasource from the master report or visa versa? ( read the masters from the sub report)

    Thanks!

    Rob




  10. Rob
    Rob avatar
    59 posts
    Member since:
    Jan 2012

    Posted 28 Aug 2013 Link to this post

    I found a way to do this..

    My solution is to pass report parameters from the master to the subreport and pass the connectionstring as a report parameter ( string)

    Remove the Datasource from the subreport so you get the 'needsDatasource' event

    Private Sub AuditDetails_NeedDataSource(sender As Object, e As EventArgs) Handles Me.NeedDataSource
        SqlDataSource1.ConnectionString = Me.ReportParameters("ConnString").Value
        Me.DataSource = SqlDataSource1
    End Sub

    And update your data source with the connectionsting passed from the master report.

    Hop this helps someone..

    Rob
  11. Mahmoud
    Mahmoud avatar
    8 posts
    Member since:
    Mar 2011

    Posted 18 Feb 2015 in reply to Rob Link to this post

    Anyone can help me achieve this:

    I have a report that is grouped by ID, in the footer of each group I need to have a TABLE that loads data from the database based on the ID of the spoken group. In the TABLE DataSource, I need to know how I can read the ID of the group. 

    Thank you very very much for you help.

    -m
  12. KS
    KS avatar
    165 posts
    Member since:
    Oct 2012

    Posted 23 Feb 2015 in reply to Mahmoud Link to this post

    Hi,

    Filter the table's data with the ReportItem.DataObject - http://www.telerik.com/help/reporting/data-items-how-to-use-data-object.html

    -KS
Back to Top
DevCraft banner