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

setting datasource of subreport and NeedDataSource event problem

10 Answers 789 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lior ef
Top achievements
Rank 1
Lior ef asked on 07 Jul 2010, 09:22 AM
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.

10 Answers, 1 is accepted

Sort by
0
Accepted
Patrick
Top achievements
Rank 1
answered on 07 Jul 2010, 01:44 PM
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);  
      }  
 
    }  
 
0
Lior ef
Top achievements
Rank 1
answered on 08 Jul 2010, 06:56 AM
hi Patrick,

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

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

0
Accepted
Peter
Telerik team
answered on 12 Jul 2010, 06:17 PM
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
0
Lior ef
Top achievements
Rank 1
answered on 14 Jul 2010, 08:35 AM
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;
}
0
Bhushan
Top achievements
Rank 1
answered on 25 Sep 2012, 11:16 AM
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.





0
Massimiliano Bassili
Top achievements
Rank 1
answered on 25 Sep 2012, 11:19 AM
c'tor = constructor, referring to the report constructor.
0
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 29 Aug 2013, 01:20 AM
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




0
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 29 Aug 2013, 02:41 AM
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
0
Mahmoud
Top achievements
Rank 1
answered on 19 Feb 2015, 01:32 AM
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
0
KS
Top achievements
Rank 1
answered on 23 Feb 2015, 12:27 PM
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
Tags
General Discussions
Asked by
Lior ef
Top achievements
Rank 1
Answers by
Patrick
Top achievements
Rank 1
Lior ef
Top achievements
Rank 1
Peter
Telerik team
Bhushan
Top achievements
Rank 1
Massimiliano Bassili
Top achievements
Rank 1
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Mahmoud
Top achievements
Rank 1
KS
Top achievements
Rank 1
Share this question
or