First, the application scenario
(1) The data content of the report needs to be filtered according to a certain parameter.
(2) This parameter is a multivalued parameter that selects one or more items from a drop-down list.
(3) The report needs to be run automatically, so the parameter must have a default value.
(4) Parameter default values cannot be determined when designing the report.
Second, the implementation of the programme
The core of the problem is that report Autorun is unattended and there is no chance of human-computer interaction, so the Required report parameter must have a default value, but some reports have parameter values that are related to the business system and cannot determine the default values for parameters when designing a report template.
The idea to solve this problem is to write a Web service dedicated to providing parameter values to the report system in the form of a JSON string, and when designing a report, set the parameter defaults to a call to the Web service instead of the normal constant value.
The specific implementation steps are as follows:
(1) Write a Web Service under the site of ActiveReports server and provide a Web method to return the JSON string.
(2) Use Report Designer to design JSON data sources and datasets.
(3) Set the default value of the multivalued parameter to the field value of the JSON dataset.
Iii. details (i) a web Service that provides a JSON data source
Under the site subdirectory of the ARS installation directory (the default is C:\ActiveReports server\), create a new Webapi.asmx file with only one line of code:
<%@ WebService language= "C #" codebehind= "~/app_code/webapi.cs" class= "WebApi"%>
To create a new WebApi.cs file in Site\app_code, the core code is as follows:
[System.Web.Script.Services.ScriptService]//This line was originally commented and must be uncomment
public class WebApi:System.Web.Services.WebService
{
...
[WebMethod]
public void Getarrayasjson ()//return value type should be a string and need to be void
{
string[] IDs = new string[]
{
"2", "4", "5", "6"//Here determine the specific return value based on the condition of the business system
};
Context.Response.Write (New JavaScriptSerializer (). Serialize (IDs));
Context.Response.ContentType = "Application/json";
Context.Response.End ()///return JSON with a direct write-back Response to avoid WebMethod returning redundant tags
return new JavaScriptSerializer (). Serialize (IDs), because the standard JSON will contain extra tags, so comment out this statement
}
Attention:
(1) The return value is an array, and the array element must be of type string, even if the data type of the database field (such as "Employee ID") that the return value is used for filtering is int and cannot be used as an integer array ({2,4,5,6}).
(2) as the content returned by the standard Web service contains a tag such as <string> such as: <string xmlns = "http://tempuri.org/" >["2", "4", "5", "6"]< /string>), and the report requires no tag data (that is, as long as ["2", "4", "5", "6"]), WebMethod cannot use the normal return statement, Instead of using the Response.Write method directly, you need to change the return value type of WebMethod from string to void.
(3) Particularly important: to ensure that the Web service can be called by the report, you need to modify the C:\ActiveReports Server\site folder under the Web. config file, add the following in the <system.web> section:
<webServices>
<protocols>
<add name= "HttpPost"/>
<add name= "HttpGet"/>
</protocols>
</webServices>
(ii) Design of JSON data sources and datasets
In Report Designer, add the data source Parameterjson:
Select the schema as "inline" and enter the following:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"Type": "Array",
"Items": {
' Type ': ' String '
}
}
In the "Content" section, select "External file or URL" and enter the following URL:
Http://localhost:8080/WebApi.asmx/GetArrayAsJson
To add a dataset parameters under the data source, in the Query section, select the [*] node:
(iii) design of multi-valued parameters and their default values
Add the parameter Empid, and select Multi-value:
Optional Values section, add the candidate parameter values, for example: 1/peter, 2/john ... etc., such as:
The default Value section, select from query statement, and then select the dataset for the previously created JSON dataset parameters:
Value field select the default Field1.
Click OK to save, and then preview the report:
Click on the parameter bar, you can see the 2,4,5,6 from the JSON data source, such as a few ID values, has been checked box to select the corresponding entry. It is equivalent to a hand-checked effect.
JSON data source provides implementation of multi-valued parameters