How to change Connection String dynamically through a report parameter

1 Answer 31 Views
.NET Core Binding DataSource SQL DataSources Expressions Rendering Report Parameters Report Viewer - ASP.NET
Trey
Top achievements
Rank 1
Trey asked on 10 Apr 2024, 04:40 AM | edited on 10 Apr 2024, 05:03 AM

Hi,

Maybe you want me refer Assign connection string dynamically using report parameter and bindings - Telerik Reporting .I followed the solution, it didn't work . And then I set the 'ConnectionString' to '@ReportParameters.PFDSConnectionString', it also didn't work, even if I add the '.Value'.Pls refer the error.png

But when I set the connection string to the property 'ConnectionString' , it worked. And I tested the parameter rendering, it's also OK. Pls refer the success.png.

I think the C# code is OK. Because when I use the appsetting.json file to injec the connection string.

Set ConnectionString to 'AcosReportsConnection', It's OK too. Pls refer the appsettings.png.

Because now the connection string come from database depending on different input parameters, I need to set the connection string dynamically.

Now I am very confused.

Do you have any suggestiones? Thanks.

 

1 Answer, 1 is accepted

Sort by
-1
Todor
Telerik team
answered on 12 Apr 2024, 02:21 PM

Hi Trey,

You need to assign the Report Parameter Value to the DataSource.ConnectionString property of the Report item/data item with Bindings rather than to the SqlDataSource component as seen on the screenshot 'error-designer.png'.

Here are alternative approaches for chanting connection strings with code:

Regards,
Todor
Progress Telerik

Stay tuned by visiting our roadmap and feedback portal pages, enjoy a smooth take-off with our Getting Started resources, or visit the free self-paced technical training at https://learn.telerik.com/.
Trey
Top achievements
Rank 1
commented on 16 Apr 2024, 03:27 AM

Hi Todor,

Thanks for your answer.

I thought the solution is too complex for me.

Perhaps I have made my problem more complex. Pls refer code.png.

I want to set the connection string from database before report initialization. And won't change it afterwards.

You mean 'DataSource.ConnectionString property of the Report item/data item with Bindings ' is?

Have other place to set the connection string?

Do you have any other more suitable solutions?

Thanks.

Best regards

Trey

Todor
Telerik team
commented on 18 Apr 2024, 10:49 AM

Hello Trey,

Based on the code I assume that you use ReportProcessor to render your reports. In this scenario, I recommend the approach from the KB article Edit the Report Data Source Components at Runtime.

Trey
Top achievements
Rank 1
commented on 30 Apr 2024, 10:14 AM | edited

I just want to transfer the connectiong string by the parameter PFDSConnectionString from backend to report.

But report cannot recognize the solution that you provide. 

Assign connection string dynamically using report parameter and bindings - Telerik Reporting

How could I set, then the telerik report could analysis the value of input parameter successfully?

Todor
Telerik team
commented on 01 May 2024, 05:32 AM

Hi Trey,

The SqlDataSource wizard in the report designers indeed requires a hard-coded connection string that may be used to fetch the schema and display the data fields in design time. It should be either a real connection string or an alias that can be resolved from the configuration of the application hosting the Reporting engine. The same is valid for the connection string of the SqlDataSource component. It cannot be set to an Expression that gets resolved in runtime.

The approach from the KB Change Connection String dynamically through a report parameter is applicable only for data items.

Based on your code, I assume that you render the report with the ReportProcessor. I suggest unpackaging the TRDP report, assigning a connection string to the SqlDataSource through the GetDataSources() method as explained in the KB Edit the Report Data Source Components at Runtime, and passing the modified report definition wrapped in an InstanceReportSource. This is the easiest approach for the particular scenario. Can you specify what problems you see with this approach?

Tags
.NET Core Binding DataSource SQL DataSources Expressions Rendering Report Parameters Report Viewer - ASP.NET
Asked by
Trey
Top achievements
Rank 1
Answers by
Todor
Telerik team
Share this question
or