ReportProcessor RenderReport PDF connectionstring in appsettings.ENV.json does not work after generating report on HTML5 Report Viewer

1 Answer 20 Views
.NET Core DataSource SQL Rendering Report Viewer - HTML5
José
Top achievements
Rank 1
José asked on 20 May 2024, 03:53 PM | edited on 22 May 2024, 01:14 PM
I'm generating a lot of reports using the ReportProcessor class and the RenderReport function to get the PDF and save it in a share drive.

private byte[] GetReport(int param)
    {
        var reportProcessor = new ReportProcessor();
        var reportSource = new UriReportSource
        {
            Uri = Path.Combine(_environment.ContentRootPath, "Reports", "Report.trdp"),
        };

        reportSource.Parameters.Add(new Parameter("param", param));

        var result = reportProcessor.RenderReport("PDF", reportSource, new Hashtable());
        return result.DocumentBytes;
    }

This works fine. But if I only run the application and get to generate the reports I get:

"An error has occurred while processing Report 'Report':
Unable to establish a connection to the database. Please verify that your connection string is valid. In case you use a named connection string from the application configuration file, make sure the name
is correct and the connection string settings are present in the configuration file of your application.
------------- InnerException -------------
Format of the initialization string does not conform to specification starting at index 0."

The part that I don't understand is that if I go to the page where I have a HTML5 Report Viewer and generate and view a report from the page the error goes away.

I search a lot about this and tried different things and got nothing.

The problem might be related with the appsettings.

We have different configuration for each env and the appsettings.json is merged with appsettings.ENV.json 

I tried to put the con string in the base appsettings and worked fine.

I need a solution because I still need to have diferent connectionstrings in different enviorments and I can't reliable run the report in the viewer before generate the standalone PDF

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 23 May 2024, 10:59 AM

Hello José,

Thank you for the sample code and the additional information!

The report displays as expected within the HTML5 Report Viewer because the Reporting REST Service renders it and it automatically accesses the loaded at application level IConfiguration instance generated from the .json configuration file where the connection string will be available.

However, when using the ReportProcessor, it is necessary to manually create and pass such an instance through the constructor. For example:

IConfiguration configuration = new ConfigurationBuilder()
	.SetBasePath(Directory.GetCurrentDirectory())
	.AddJsonFile("appsettings.json")
	.Build();

var reportProcessor = new ReportProcessor(configuration);

 Please give this approach a try and let me know how it goes.

Regards,
Dimitar
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/.
Tags
.NET Core DataSource SQL Rendering Report Viewer - HTML5
Asked by
José
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or