Subreport - ObjectDataSource set DataSource parameter programmatically

4 Answers 471 Views
.NET Framework DataSource Object DataSources Programming Rendering Report Parameters Serialization SubReport
Michal
Top achievements
Rank 1
Iron
Michal asked on 16 Jun 2021, 12:05 PM

Hi

I'm developing an WPF app that imports trdx file, serializes to a report object, set an ObjectDataSource and generate (using RenderReport method) to pptx file.

The trdx file contains a subreport with the same DataSource but different DataMember. The DataMember of the subreport get an input from the a report parameter (=Parameters.Param1.Value). The subreport parameter filled by the main report (Param1 =Fields.Id).

For example, for each order in the report, the subreport get its ID as parameter and returns all its order details.

In the generated report (pptx) I see all the reports data but the subreport data is empty. How should I set the DataSource, DataMember and Paramerters of the subreport correctly?

 

Thanks

Michal

 

4 Answers, 1 is accepted

Sort by
0
Accepted
Michal
Top achievements
Rank 1
Iron
answered on 07 Jul 2021, 10:50 AM

Hi Neli,

I found the issue.. in BindDataToSubReports i should keep the subreport parameters before i override the report source

                var temp = subReport.Parameters;
                subReport.ReportSource = instanceSubReport;
                subReport.Parameters.AddRange(temp);

 

this is the right way to do it?

 

Thanks

Michal


Neli
Telerik team
commented on 12 Jul 2021, 01:41 PM

Hello Michal,

Actually, in the code, I noticed that you are using this piece of code:

var tempDataSource = (ObjectDataSource)tempReport.DataSource;
var tempParameters = tempDataSource.Parameters;
foreach (var parameter in tempParameters)
{
    objectDataSource.Parameters.Add(parameter);
}
tempReport.DataSource = objectDataSource;

var instanceSubReport = new InstanceReportSource() {ReportDocument = tempReport};
subReport.ReportSource = instanceSubReport;

I made some changes in order to run the application, however, I got the following output. I tried to debug the application, but it seemed that the BindDataToSubReports method was not called. Can you please let me know what's the outcome on your end?

Michal
Top achievements
Rank 1
Iron
commented on 20 Jul 2021, 08:14 AM

Hi Neli,
Sorry for the delay.
The attached sln does not include the code i worte in my last comment (i found it after i zipped the sln).
BindDataToSubReports method called in GenerateReport (MainWindow.xaml.cs line 91) only if your template (.trdx) contains sub report item.

Michal
Todor
Telerik team
commented on 27 Jul 2021, 12:31 PM

Indeed, the code sets the subreport ObjectDataSource parameter value to '=Parameters.ID.Value', hence it expects a valid Report Parameter value for the SubReport > ReportSource. However, the InstanceReportSoruce created in the code that overrides the original UriDataSource doesn't provide such a value, and there is no default value in the subreport. For that reason, the subreport renders empty.

Here is the single line I suggest adding to the method BindDataToSubReports:

protected void BindDataToSubReports(ObjectDataSource objectDataSource, SubReport subReport)
{
    ...

        var instanceSubReport = new InstanceReportSource() {ReportDocument = tempReport};
        instanceSubReport.Parameters.AddRange(subReport.ReportSource.Parameters);
        subReport.ReportSource = instanceSubReport;
    }
}

Michal
Top achievements
Rank 1
Iron
commented on 30 Jul 2021, 07:52 AM

Thanks! That solved the issue
0
Todor
Telerik team
answered on 21 Jun 2021, 07:01 AM

Hi Michal,

Generally, the subreport DataSource should be set to an ObjectDataSource with the corresponding DataSource and DataMember. The ObjectDataSource parameter of the subreport should be set to the subreport parameter that is received from the main report with the SubReport > ReportSource. This may be done inside the subreport definition, for example, in its TRDX file.

If the subreport DataSource should be set dynamically, like the main report one, then you must deserialize the subreport, set its DataSource, and pass the subreport instance wrapped in an InstanceReportSource to the main report SubReport > ReportSource.

Regards,


Todor
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
0
Michal
Top achievements
Rank 1
Iron
answered on 21 Jun 2021, 08:17 AM

Hi Todor,

Thank you for the reply.

I think this is exact what I did but its not working.

I attached my code.

 

Thanks

Michal

Neli
Telerik team
commented on 24 Jun 2021, 01:00 PM

Hi Michal,

I am sorry to hear that the problem is not resolved yet. Can you please put some breakpoints and try to debug the code? Please, check which part does not work- is it setting the DataMember parameter value or assigning the datasource? It will help us a lot if you can send us a sample runnable application that reproduces the issue. 

0
Michal
Top achievements
Rank 1
Iron
answered on 27 Jun 2021, 05:45 AM

Hi Neli

 

I Attached the .sln file and the .tdrx files that I used

Thanks

Michal

Neli
Telerik team
commented on 06 Jul 2021, 11:53 AM

Hi Michal,

Thank you for the provided project. I inspected it, changed some of the paths, but for some, reason, I am afraid that I was not able to run it. Can you please confirm that it works on your end? Maybe you can send me a video that demonstrates the issue. As I mentioned, you can try to debug, for example, you can put a breakpoint in the BindDataToSubReports method and what actually is returned from it.

Tags
.NET Framework DataSource Object DataSources Programming Rendering Report Parameters Serialization SubReport
Asked by
Michal
Top achievements
Rank 1
Iron
Answers by
Michal
Top achievements
Rank 1
Iron
Todor
Telerik team
Share this question
or