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

Set SubReport DataSource Programmatically

13 Answers 2605 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Reggie Gardner
Top achievements
Rank 1
Reggie Gardner asked on 07 Nov 2007, 09:05 PM
What is the method for setting the data source for subreports?

I currently have a report with 2 subreports. At runtime I set the report's data source to a DataSet. I also set the data source for the 2 subreports to the same DataSet. No data appears to be getting to the subreports.

public PeakReport(DataSet ds)
{
            /// <summary>
            /// Required for telerik Reporting designer support
            /// </summary>
            InitializeComponent();

            DataSource = ds;

            subAMDaily.Report.DataSource = ds;
            subPMDaily.Report.DataSource = ds;
}

13 Answers, 1 is accepted

Sort by
0
Ivan
Telerik team
answered on 08 Nov 2007, 03:20 PM
Hello Reggie Gardner,

To drive data into subreports there are three ways:

1. Using Report Filters.
  • In the report which will serve as sub report (some times named Slave Report):
    • Edit DataSource property to connect report to the data;
    • Edit ReportParameters property to create proper parameters;
    • Edit Fileters property to create data source filtering criteria.
  • In the report which will serve as master report:
    • Add Subreport item and assign it's ReportSource property to the report described above;
    • Edit subreport's Parameters property to bind parameters of slave report to data fields of master report.
Please refer to the Product Line Sales Report example (Telerik | Reporting | Examples | C# Solution):
  • First preview the TopEmployees report and initialization of it's DataSource, Filters and ReportParameters properties;
  • Then look how the master report (ProductLineSales) drives the TopEmployees through subreport's Parameters property.
2. Using NeedDataSource event handler.
In Master report opened in report designer select the Subreport item and create a NeedDataSource event handler. In this event handler we are initializing the sub report's DataSource property. We can use command parameters to filter the data:
Telerik.Reporting.Processing.ReportItemBase item = sender as Telerik.Reporting.Processing.ReportItemBase; 
DataRowView dataRow = (DataRowView)item.DataItem; 
string commandText = @"SELECT id, name, invoiceID FROM theTable WHERE groupID=@parameterID"
SqlDataAdapter adapter = new SqlDataAdapter(commandText, connectionString); 
adapter.SelectCommand.Parameters.AddWithValue("@parameterID", dataItem["InvoiceID"]); 
this.DataSource = adapter; 
 
where InvoiceID is real column name from master report's data source.

In already mentioned Product Line Sales Report example you can preview chart1_NeedDataSource method of ProductLineSales report where we initialized the chat's data source in same fashion.
3. Using ItemDataBinding event handler.
  • Create Slave report and connect its DataSource through SqlDataAdapter with command parameter as demonstrated here and expose the parameter through public property;
  • In Master report opened in report designer select the Subreport item and create a ItemDataBinding event handler. In this event handler we are initializing the sub report's command parameter.
Please preview attached 107491B sample solution.

About using one DataSet object as data source for master and sub reports. Probably the empty reports in your case are result of not filled DataSet object. Please preview attached 107491A sample solution where three-level hierarchy is displayed.

Note: refer to Performance Considerations before you decide which method to implement in your case.

Please refer following help-topics for more information as well: Adding a Data Source Programmatically, Filtering Report Data, Edit Filter Dialog, Creating Master-Detail Reports Using SubReports, Display Hierarchical Data with SubReports.

I hope this information helps.

Regards,
Ivan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Reggie Gardner
Top achievements
Rank 1
answered on 08 Nov 2007, 06:22 PM
This application works by having a service create the DataSet with the report data and write the DataSet to an xml file.

The web report then creates a DataSet from the xml file and uses that DataSet for the data source of the report.

The master report works great. But the subreports don't get any data. I will need to use report parameters and filters, but so far even without them they don't get any data. I know the xml file contains the data for the table used by the subreports. I also tried writing the DataSet back to xml after loading it from the xml file and all the data is still present. So I don't think it's a problem with the DataSet itself.

I'd like to verify that setting the subreports data source in this manner is appropriate:

subAMDaily.Report.DataSource = ds;

I tried using the NeedDataSource event, but with the same result of no data.
The only suspicious thing I've noticed is that the master report fields work whether I set the DataMember property to the table name or not.
0
Reggie Gardner
Top achievements
Rank 1
answered on 08 Nov 2007, 07:13 PM
I tried exporting the report to pdf and I got the following error for every field in the subreport:

#ERROR # The expression contains object 'speed_avg' that is not defined in the current context.
0
Ivan
Telerik team
answered on 12 Nov 2007, 12:42 PM
Hello Reggie Gardner,

I have already sent you an answer to your support ticket ("Data source for subreports"), with a working sample attached. Please check it and let me know if I can be of any help.

Regards,
Ivan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Reggie Gardner
Top achievements
Rank 1
answered on 14 Nov 2007, 05:37 PM
For anyone looking for the answer...

Instead of this code:

subAMDaily.Report.DataSource = ds;
subPMDaily.Report.DataSource = ds;

I needed to do this:
subAMDaily.ReportSource.DataSource = ds;
subPMDaily.ReportSource.DataSource = ds;

0
Michael
Top achievements
Rank 2
answered on 29 Jan 2009, 07:15 PM
I found this advice (the second post, and official response) to be very helpful, and think that the contents of the post above should find their way into the documentation in a more formal way.
0
Steve
Telerik team
answered on 30 Jan 2009, 02:36 PM
Hi Michael,

Thank you for the heads up. I would make sure these are included in our documentation for the next release.

Kind regards,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Amanda
Top achievements
Rank 2
answered on 07 Dec 2009, 06:21 PM
Okay, so in the second post, I'm trying out number 2.  Here's my code in the master report:

Private Sub SubReport1_NeedDataSource(ByVal sender As ObjectByVal e As System.EventArgs) Handles SubReport1.NeedDataSource  
 
 
        Dim item As Telerik.Reporting.Processing.ReportItemBase = TryCast(sender, Telerik.Reporting.Processing.ReportItemBase)  
        Dim dataRow As DataRowView = DirectCast(item.DataItem, DataRowView)  
        Dim commandText As String = "SELECT * FROM tblERmain WHERE er_id=@er_id" 
        Dim connectionString As SqlConnection = GetSqlConnection()  
        Dim adapter As New SqlDataAdapter(commandText, connectionString)  
        Dim testFlag As String = item.DataObject("er_id")  
        adapter.SelectCommand.Parameters.AddWithValue("@er_id", item.DataItem("er_id"))  
        'SubReport1.ReportSource.DataSource = adapter  
 
 
        Dim dt As DataSet = New DataSet  
        adapter.Fill(dt, "ERtotals")  
        SubReport1.ReportSource.DataSource = dt  
        Dim flag As String = "" 
         
 
    End Sub 

I know that the adapter and dataset are grabbing the correct information.  The problem is that when the report shows up, the subreport is not showing the information from the database.  I'm not sure why.  In the master report in the subreport1 properties I have the ReportSource pointing to my class library and the correct report.  With this, the database info just doesn't show up.  When I have the reportsource not pointing to anything, then nothing shows up, which makes sense.  But what could I be doing incorrectly that the information isn't showing? 
0
Steve
Telerik team
answered on 08 Dec 2009, 01:37 PM
Hi Amanda,

If SubReport1 is the name of the subreport item itself, then the NeedDataSource should look like this:

Telerik.Reporting.Processing.SubReport subReportItem = sender as Telerik.Reporting.Processing.SubReport;
subReportItem.InnerReport.DataSource = dt

Note that we use the processing subreport object and the InnerReport property to access the report used as subreport.


Sincerely yours,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michael
Top achievements
Rank 1
answered on 16 Jul 2012, 08:13 PM
Hi

I have a Report with a Subreport, both have different object DataSources; reports are displayed in an web page where DataSources are set-up. In other words I must be able to set DataSource for both reports (Master & Slave) from web-page.
How can I do that?

Thanks!
0
Elian
Telerik team
answered on 18 Jul 2012, 12:44 PM
Hello Michael,

To be able to modify items in a report that serves as a sub-report, you will have to modify a bit your application. Since the introduction of the new Report Sources, the ReportSource property of theSubReport item is now of type ReportSource (not Report like before). 

Here is a sample code that reflects the changes:

//instantiate new report - the one that will serve as a subreport
var subReport = new DetailReport();
//set the DataSource
subReport.DataSource = ...
  
//instantiate the master report
var masterReport = new MasterReport();
//set the master report's DataSource
masterReport.DataSource = ...
//find the subreport item inside the master report
var subRepItem = report.Items.Find(typeof(Telerik.Reporting.SubReport), true)[0] as Telerik.Reporting.SubReport;
//set it's report source to a new InstanceReportSource with the report document that you have already instantiated above
subRepItem.ReportSource = new InstanceReportSource { ReportDocument = subReport };
//display the master report in the viewer
this.ReportViewer1.ReportSource = new InstanceReportSource { ReportDocument = masterReport };

If you are already using InstanceReportSource in the master report, then you can take the SubReportItem.ReportSource and cast it directly (instead of instantiating new DetailReport) if that works better for you. 
 
All the best,
Elian
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

0
Rick
Top achievements
Rank 2
Iron
answered on 21 Sep 2012, 10:03 PM
Telerik.Reporting.Processing.ReportItemBase item = sender as Telerik.Reporting.Processing.ReportItemBase; 
DataRowView dataRow = (DataRowView)item.DataItem; 

Is there a way to accomplish this in the current version? I was unable to get this code to work so that i could get the value of cell in the current row.

Something similar is actually done here in this video at around 14:30 as well and I was unable to reproduce it.

http://tv.telerik.com/watch/reporting/video/working-with-charts-telerik-reports 
0
Elian
Telerik team
answered on 24 Sep 2012, 02:43 PM
Hello Rick,

the DataItem property was deprecated a couple of years ago. It has been replaced by the DataObject property
For more info see:
 
Greetings,
Elian
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

Tags
General Discussions
Asked by
Reggie Gardner
Top achievements
Rank 1
Answers by
Ivan
Telerik team
Reggie Gardner
Top achievements
Rank 1
Michael
Top achievements
Rank 2
Steve
Telerik team
Amanda
Top achievements
Rank 2
Michael
Top achievements
Rank 1
Elian
Telerik team
Rick
Top achievements
Rank 2
Iron
Share this question
or