How to get the environment at execution for FastReport
We are using FastReport with our on-premise ERP software.
Reports are designed with the FastReport Desktop (version 2021.4.15).
The reports are executed from within the ERP client which runs in the browser.
As we have multiple instances of the ERP (Dev, Test, Training, Production, ...) we need to include the environment in which a report is being executed.
I have looked at using the Report or Engine classes but I can't find any property or method that could give me a hint: I would be happy if I could retrieve the URL or server name or just anything that could help in distinguishing the environment.
This discussion has been closed.
Comments
I think you can make use of the parameters available in the reports.
And the .NET code for set parameter value.
Report reportFRX;
reportFRX.Load("XXXXXX.report.frx");
reportFRX.SetParameterValue("environment", "The value environment by appsetting.json, web.config, other");
reportFRX.SetParameterValue("executionGuid", Guid.NewGuid().ToString());
reportFRX.RegisterData(ds); //load data set
.... other code
reportFRX.Prepare();
--------------------------------------------------
The answer is a little late, but I hope it helps someone else.
😉
Thanks, any input welcome.
If I'm correct SetParameterValue assigns a value to a parameter? I am wondering where/how I could retrieve a value from a config file? Just note that we are running the report from an ERP application, we don't have own code that launches the report. We can pass a value to a parameter but would need to do that for every report separately.
Hello!
Even if an external ERP system runs templates without your C# code and does not pass custom parameters via `SetParameterValue`, within the .frx script itself (for example, in `_StartReport`) you can retrieve the runtime environment directly in one of three ways:
private void _StartReport(object sender, EventArgs e) { // 1. Server/host name or OS environment variable: string host = System.Environment.MachineName; string envVar = System.Environment.GetEnvironmentVariable(‘ASPNETCORE_ENVIRONMENT’) ?? ‘’; // 2. Server/database name from the connection string within the report itself: string connStr = Report.Dictionary.Connections.Count > 0 ? Report.Dictionary.Connections[0].ConnectionString : ‘’; TextEnv.Text = host + ‘ | ’ + connStr; }If the ERP system substitutes the connection string according to the environment (Dev / Test / Prod), checking Report.Dictionary.Connections[0].ConnectionString is the most reliable way to determine the current environment directly within the report.