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

How do you programmatically bind subreports in Q2 2012?

11 Answers 474 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joshua
Top achievements
Rank 1
Joshua asked on 16 Jul 2012, 09:49 PM
I just "upgraded" to Reporting Q2 2012 last week and have spent the past few hours trying to get my reports working again (the upgrade wizard didn't fully upgrade the reports for some reason, so I had to rebuild them). I finally got my main reports figured out, but haven't been able to determine what to do to bind subreports. For example, I have an invoice report that has a subreport in the Details section that I use to display line items. I need to bind the main report and subreport to an in-memory dataset - the dataset contains two tables, a header table and an items table. In the previous Reporting version (Q1 2012), I was able to do the following to bind the report and subreport:

Quote tst = new Quote(); //Quote is the main report
Telerik.Reporting.ObjectDataSource odsHeader = (Telerik.Reporting.ObjectDataSource)tst.DataSource;
odsHeader.DataSource = cdtsQuote;
odsHeader.DataMember = "Header";
 
Telerik.Reporting.SubReport itemDetail = (Telerik.Reporting.SubReport)tst.Items.Find("srItemDetail", true)[0];
Telerik.Reporting.ObjectDataSource odsItems = (Telerik.Reporting.ObjectDataSource)itemDetail.ReportSource.DataSource;
odsItems.DataSource = cdtsQuote;
odsItems.DataMember = "OrderItems";

In the new version, the SubReport object's ReportSource has changed, so it doesn't have a DataSource property anymore. So, how do I go about setting the datasource? If I try setting the subreport's datasource via SubReport.Report.DataSource, it ends up re-setting the datasource of the master report, which causes even more problems.

Any ideas on how to accomplish this?

11 Answers, 1 is accepted

Sort by
0
Accepted
IvanY
Telerik team
answered on 18 Jul 2012, 10:50 AM
Hi Joshua,

The ReportSource property of the SubReport has indeed changed and this gives you the possibility to assign a report not just by using a report instance, but also uri, xml, etc. For more information you can check our Report Sources help article.

Now because of these changes the ReportSource no longer exposes the properties you are used to and because of that you have to approach the issue in a different manner. In the case with the DataSource you will have to do it like this:

var tst = new Quote();
  
// Deal with the main report data source the same way
var itemDetail = (Telerik.Reporting.SubReport)tst.Items.Find("srItemDetail", true)[0];
var repSource = (InstanceReportSource)itemDetail.ReportSource;
  
// Because you know the ReportDocument is a Report;
var report = (Telerik.Reporting.Report)repSource.ReportDocument;
var odsItems = (Telerik.Reporting.ObjectDataSource)report.DataSource;
odsItems.DataSource = cdtsQuote;
odsItems.DataMember = "OrderItems";

Also you have to know that the Report property is actually the containing report of the SubReport item and not the detail report assigned - this is just to be clear, although you probably figured it out on your own.

Kind regards,
IvanY
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
Larry
Top achievements
Rank 1
answered on 28 Sep 2012, 08:15 PM
Ok I am beyond frustrated
After hours of trying to fix my reports after the Q2 2012 update I still have a mess.
Can some one please tell me how to programmatically bind my subreports in this release.

My existing code was:
Dim rptLvlSex As Telerik.Reporting.SubReport = TryCast(rpt.Items.Find("SubLevelSex", True)(0), Telerik.Reporting.SubReport)
rptLvlSex.ReportSource.DataSource = dsCS

What is the new VB code that I need to use
Thank you
I sure hope Q2 2012 is worth the effort :-(

Update:
Ok while waiting around for a response I was able to get it to work.
Not sure if this is the perferred or best way, but it seems to work.

Dim rptLvlSex As Telerik.Reporting.SubReport = TryCast(rpt.Items.Find("SubLevelSex", True)(0), Telerik.Reporting.SubReport)

Dim iRS2 As Telerik.Reporting.InstanceReportSource = TryCast(rptLvlSex.ReportSource, Telerik.Reporting.InstanceReportSource)

 

Dim sub2 As Telerik.Reporting.Report = TryCast(iRS2.ReportDocument, Telerik.Reporting.Report)

sub2.DataSource = dsCS


This really doesn't seem like a very elegant solution with all this Casting, so if there is a better soloution, please let me know.
0
CB
Top achievements
Rank 2
answered on 29 Sep 2012, 09:52 PM
I feel your pain Larry - this is doing my head in too.
0
IvanY
Telerik team
answered on 03 Oct 2012, 12:17 PM
Hello everyone,

The Report Sources abstraction may seem as an obstacle at first, but once you check the help resources and grasp the main idea behind that abstraction you will see that it actually gives you more flexibility when you access your reports.

As reports can be stored anywhere there should exist an uniform way to access them - and this is exactly what Report Sources do. Previously the report viewers and the subreports were only able to use an instance of your report class. However this changed with the Report Sources, which now give you the opportunity to access your reports in the following ways:
  1. Instance - this is the way the reports used to be accessed before Q2 2012 (using and instance of your report class).
  2. Type - you can access your reports declaratively, only by specifying the type.
  3. URI - this gives you the option to load your report either from a file path or an Url
  4. XML - it can be used for reports stored in databases as pure XML and can be used like this: xmlReportSource.Xml = "=Fields.ReportXml"

Additionally you can check the How to migrate your project to utilize the new ReportSource objects KB article and the Report Sources help article which can give you a more detailed explanation about the Report Sources.

Greetings,
IvanY
the Telerik team

HAPPY WITH TELERIK REPORTING? Do you feel that it is fantastic? Or easy to use? Or better than Crystal Reports? Tell the world, and help fellow developers! Write a short review about Telerik Reporting and Telerik Report Designer in Visual Studio Gallery today!

0
Stephen
Top achievements
Rank 1
answered on 03 May 2013, 09:10 AM
Hi

I have a report which has a subreport defined referenced by a UriReportSource

I am using the RenderReport to create a PDF and I can loop through the main report and change any connection strings to a new connection string which allows me to have the same report but point it at different databases for different customers.

Problem I have is the sub report. As its a UriReportSource I cannot find any events to tap into to allow me to alter the connection strings for the Data sources when it gets rendered.

Would appreciate any help you can give as this is a major stumbling block.

Steve
0
Peter
Telerik team
answered on 06 May 2013, 07:16 AM
Hello Steve,

Our advice is to is to utilize an user function to update the connection strings as shown in the following example:

public static SqlDataSource UpdateConnectionString(SqlDataSource dataSource)
{
    dataSource.ConnectionString = "New Connection String";
    return dataSource;
}

Then you have to add the following binding to the data items: 

 Property path

Expression

DataSource

 = UpdateConnectionString(ReportItem.ItemDefinition.DataSource)

This binding will get the current data item data source component, execute the user function and bind the current report item to the updated data source component.

Additionally you may find useful the Extending Report Designer help article.

Kind regards,
Peter
the Telerik team

Have you tried the new visualization options in Telerik Reporting Q1 2013? You can get them from your account.

0
Stephen
Top achievements
Rank 1
answered on 06 May 2013, 09:06 PM
Hi Peter

Many thanks for the reply. I am a bit confused though.

I have two trdx files. One has the other as a sub report referenced by a UriReportSource. Where do I add the code for:

public static SqlDataSource UpdateConnectionString(SqlDataSource dataSource)
{
    dataSource.ConnectionString = "New Connection String";
    return dataSource;
}

Kind regards
Steve
0
Peter
Telerik team
answered on 07 May 2013, 11:49 AM
Hi Steve,

How to extend the standalone report designer with user functions is elaborated in the Extending Report Designer help article.

Give it a try and let us know how it goes.

Greetings,
Peter
the Telerik team

Have you tried the new visualization options in Telerik Reporting Q1 2013? You can get them from your account.

0
Stephen
Top achievements
Rank 1
answered on 08 May 2013, 08:56 AM
Many thanks Peter.

Worked perfectly.

Thanks for your assistance.

Steve
0
Robot B9
Top achievements
Rank 2
answered on 13 Sep 2017, 12:01 PM

When I tried your sample code above, this didn't work.
My following code after conversion is causing the following error.

Unable to cast object of type 'Telerik.Reporting.TypeReportSource' to type 'Telerik.Reporting.InstanceReportSource'.

Dim srReq = DirectCast(Me.Items.Find("srRequirements", True)(0), Telerik.Reporting.SubReport)
'This line is one erroring.
Dim repSource = DirectCast(srReq.ReportSource, InstanceReportSource)

Dim report = TryCast(repSource.ReportDocument, Telerik.Reporting.Report)
Dim odsItems = DirectCast(report.DataSource, Telerik.Reporting.ObjectDataSource)
odsItems.DataSource = Me.DataSource
odsItems.DataMember = "REQUIRED_RECS"

The previous code, below, before the ReportInstance worked fine for the subreport bindings.
I just can't seem to figure how to bind this .

Me.SupplierSubRequirement1.Bindings.Add(New Telerik.Reporting.Binding("DataSource", "=Fields.REQUIRED_RECS"))



0
Katia
Telerik team
answered on 18 Sep 2017, 08:57 AM
Hi Robot B9,

It will not be possible to convert TypeReportSource to InstanceReportSource. To make modifications to the sub report instance you can create a new sub report, access/modify its properties and then set it as ReportSource for the SubReport item:
Dim mainReportInstance = New MasterReport()
Dim subreportItem = TryCast(mainReportInstance.Items.Find("subReport1", True)(0), Telerik.Reporting.SubReport)
Dim subreport = New SubReport1()
subreport.DataSource = "new data source"
subreportItem.ReportSource = New InstanceReportSource() With { _
    Key .ReportDocument = subreport _
}


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
Tags
General Discussions
Asked by
Joshua
Top achievements
Rank 1
Answers by
IvanY
Telerik team
Larry
Top achievements
Rank 1
CB
Top achievements
Rank 2
Stephen
Top achievements
Rank 1
Peter
Telerik team
Robot B9
Top achievements
Rank 2
Katia
Telerik team
Share this question
or