Tuesday, September 30, 2014

Telerik Reporting: set subreport datasource the same as main report

Telerik had some posts regarding this, however the post is not clear enough so I have to spend some time to figure out myself.

Here are the steps I have made the subreport datasource dynamically configured (Telerik Reporting Q1 2014) successfully:

Step 1. Define a static method in your factory class:
public static class YourFactoryClass{
        public static Telerik.Reporting.SqlDataSource SetConnectionString(Telerik.Reporting.SqlDataSource dataSource)
        {
 
            var dataDBConnectionString = _get_DB_connect_str();
            dataSource.ConnectionString = dataDBConnectionString;
            return dataSource;
        }
 }

Step 2. Expose your data with custom User Functions, you have to use the AssemblyReferences Element of the Telerik.Reporting configuration section to reference your custom assembly:
in your web.config or app.config:
<configuration>
      <configsections>
          <section allowdefinition="Everywhere" allowlocation="true" name="Telerik.Reporting" type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting">
          </section>

      </configsections>
      …
       <telerik .reporting="">
          <assemblyreferences>
              <add culture="neutral" name="YourCustomAssembly_Name" publickeytoken="null" version="1.0.0.0"/>              
          </assemblyreferences>        
       </Telerik.Reporting>
</configuration>


Step 3. Add DataSource binding in your subreport ( not the main report) data items property. In Report Designer, right click anywhere other than those fields defined, find 'Bindings' inside Properties panel then launch the 'Edit Bindings' dialogue box,  and 'DataSource' should be in the Property path dropdown so just select it from dropdown:

Property path: DataSource
Expression: =XXXNameSpace.YourFactoryClass.SetConnectionString(ReportItem.ItemDefinition.DataSource)

Step 4. Rebuild your project/solution, have it a try. It should hit the static method defined ( to dynamically update the connection string for subreport ) in the step 1 if you run in debugging mode.

That is it.

No comments:

Post a Comment