Changing Datasource at runtime in Blazor

0 Answers 198 Views
Report Viewer - Blazor
Eli
Top achievements
Rank 1
Veteran
Eli asked on 13 Jan 2022, 04:05 PM

I have a large library of .Net 4+ reports and I want to upgrade them in such a way that they will work in a Blazor Report Viewer.

If it is a simple report with parameters, I can convert to .trdp fine and it works.  However, I have a large number of reports that are much more complex.  I am used to loading them in as an InstanceReportSource in code behind (WebForms project) and doing a variety of things to them, one big one is specifying specific data sources at runtime and then loading the report.

I am finding minimal examples on how to do anything in Blazor, but I will start with this question:  How do I change/specify a datasource for a report at runtime and then display the report in Blazor ReportViewer?

 

 

Neli
Telerik team
commented on 18 Jan 2022, 11:30 AM

Hi Eli,

For .NET Core applications, you can use the approach from the Action NavigateToReport does not work after updating the Connection String dynamically in a Custom Report Resolver KB article.

In the Resolve() method, you need to use the passed report string to identify the Report definition. For example, if the report argument is the path to a TRDP report, you may wrap it in a UriReportSource and pass it to the UpdateReportSource method of the ReportConnectionStringManager. Note that you need to modify the connection string when operationOrigin is CreateReportInstance and GenerateReportDocument. In the first case for fetching parameter values, in the second for generating the report document. Here is a sample code for the Custom ReportSource Resolver:

public class MyReportSourceResolver : IReportSourceResolver
{
    string connectionString = null;
	
    public MyReportSourceResolver(string connectionString)
    {
        this.connectionString = connectionString;
    }
	
    public ReportSource Resolve(string report, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
    {
        var uriReportSource = new UriReportSource()
        {
            Uri = Path.Combine(@"path to the reports here", report)
        };

        ReportSource reportSource = uriReportSource;

        if (operationOrigin == OperationOrigin.CreateReportInstance || operationOrigin == OperationOrigin.GenerateReportDocument)
        {
            var manager = new ReportConnectionStringManager(this.connectionString);
            reportSource = manager.UpdateReportSource(uriReportSource);
        }

        return reportSource;
    }
}

And here is how you may assign the custom resolver to the REST Service configuration:

services.TryAddSingleton<IReportServiceConfiguration>(sp =>
    new ReportServiceConfiguration
    {
        ReportingEngineConfiguration = sp.GetService<IConfiguration>(),
        HostAppId = "Html5DemoAppCore",
        Storage = new FileStorage(),
        ReportSourceResolver = new MyReportSourceResolver("Telerik.Reporting.Examples.CSharp.Properties.Settings.TelerikConnectionString")
    });

I hope the above information will help. Let us know if further questions arise.

No answers yet. Maybe you can help?

Tags
Report Viewer - Blazor
Asked by
Eli
Top achievements
Rank 1
Veteran
Share this question
or