How to change ConnectionString in Blazor Report?

1 Answer 262 Views
General Discussions Report Viewer - Blazor
Heejin
Top achievements
Rank 1
Heejin asked on 17 May 2022, 07:52 AM

hello.

I have a question about specifying the report source.

 

in razor page code : 

<ReportViewer @ref = "ReportViewer1"
	ReportSource=@@@
	...
>

 

Only ReportSourceOptions were available for ReportSource in the ReportViewer.

Report in ReportSourceOptions can only be fileName with String.

 

What I want is to use UriReportSource to deliver ConnectionString or change DataSource.

Therefore, I would like to use ReportSource instead of fileName.

The document I referred to is as follows.

1. Action NavigateToReport does not work after updating the Connection String dynamically in a Custom Report Resolver

2. Changing Datasource at runtime in Blazor

 

I referred to the second document and thought that using resolver could solve the problem that ReportSourceOption does not receive the ReportSource format.

But the problem still hasn't been solved.


ReportSourceOptions rso = new ReportSourceOptions();
CutsomReportSourceResolver sr = new CustomReportSourceResolver("connectionStrings");
rso = sr.Resolver("Report.trdx", new Dictionary<string, object>{ @@@}); (X)
//OperationOrigin is skip

Please let me know if I used the wrong method.

Additionally, I don't know what Operation Origin is like.

Also I would be grateful if you would give us about operationorigin.

 

thanks.

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 19 May 2022, 01:48 PM

Hi Heejin,

Thank you for the provided information and code!

The problem with your current setup is that, from what I understand, you are trying to use the custom resolver with the Blazor report viewer on the front end.

The custom IReportSourceResolver implementation is meant to be used at the server-side by the Reporting REST Service. The custom resolver should be initialized as the value of the ReportSourceResolver property of the ReportServiceConfiguration.

In .NET Core projects, this configuration is usually done in Startup.cs inside the ConfigureServices method. For example:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddRazorPages()
                    .AddNewtonsoftJson();
            services.AddServerSideBlazor();

            // Configure dependencies for ReportsController.
            services.TryAddSingleton<IReportServiceConfiguration>(sp =>
                new ReportServiceConfiguration
                {
                    ReportingEngineConfiguration = sp.GetService<IConfiguration>(),
                    HostAppId = "Net5BlazorDemo",
                    Storage = new FileStorage(),
                    ReportSourceResolver = new MyReportSourceResolver("connection_string"),
                });
                ....
           }

Once you have edited this setup to use your custom resolver, the work is done. Any ReportSourceOptions.Report that you pass will have its connections edited in the resolver and will be then sent to the viewer. So setting that to a filename is expected here.

ReportSource="@(new ReportSourceOptions
                              {
                                  Report = "Report.trdp",
                              })"

The Resolve method will receive this Report property as the reportId parameter, will create an UriReportSource for it, will edit the connection strings, and will then return it to the viewer with an updated connection.

For more information on this, please go through the How to use Custom Report Source Resolver and Custom Report Document Resolver article.

Thank you for using Telerik Reporting!

Regards,
Dimitar
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/.
Heejin
Top achievements
Rank 1
commented on 23 May 2022, 12:22 AM

Dear Dimitar


Thank you for your reply.


I am now well aware that I have to set up the Connection string at Startup.cs, and the Resolver works on Starterup.cs through your feedback.


I'd like to carry on with an additional job such as changing the Dataset by designating it on the Report.


I'd like to make an inquiry on how I could send the Dataset while declaring the Reportviewer at the same time.


Do I have to sign up an additional Report Source Resolver just like how I changed the Connection String?


In VB.net, I loaded the Report Connection String Manager and changed the Datasource of the report with the Deserialized Uri Report Source.

code :

Dim connectionStringHandler = New ReportConnectionStringManager(gConnectionString)

'Added Elements) Changing DataSource Elements in a Report
Dim sqlString = "select top 1 first_name From t_user where user_id = @user_id"
Dim connectionStringHandler = New ReportConnectionStringManager(gConnectionString, Telerik.Reporting.SqlDataSourceCommandType.Text, sqlString)
connectionStringHandler.addSQLParameter("@user_id", DbType.String, "user_id")

Dim reportSource = connectionStringHandler.UpdateReportSource(source)

ft.ReportViewer1.ReportSource = reportSource
ft.ReportViewer1.RefreshReport()

 

However, I am currently lost and not sure of what to do.


I need your help desperately.


Thank you.

Dimitar
Telerik team
commented on 25 May 2022, 02:42 PM

If I understand correctly, you need to change the SQL query that will be executed, is that correct?

You can do this by using the Report.GetDataSources Method to get all/some data sources of type SqlDataSource, then you may edit the SqlDataSource.SelectCommand Property directly. For example:

var query = "SELECT [HumanResources].[vEmployee].[EmployeeID] FROM[HumanResources].[vEmployee] WHERE[HumanResources].[vEmployee].[EmployeeID] <= 50";
            var reportPackager = new ReportPackager();
            var reportProcessor = new ReportProcessor();
            Telerik.Reporting.Report report = null;
            using (var sourceStream = System.IO.File.OpenRead("Report1.trdp"))
            {
                report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
            }

            var sqlDS = report.GetDataSources().OfType<SqlDataSource>();
            foreach (var sqlDataSource in sqlDS)
            {
                 //you can use this approach to edit the connection string too
                //sqlDataSource.ConnectionString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=SSPI";
                sqlDataSource.SelectCommand = query;
            }

The other option is a binding to the SqlDataSource.SelectCommand as explained in the Change Connection String dynamically through a report parameter KB article.

Tags
General Discussions Report Viewer - Blazor
Asked by
Heejin
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or