In a Section report, you can use the Parameters collection to pass values directly into a control, or you can also use it to display a subset of data in a particular instance of the report.
There are several ways to set up parameters in a report:
When you add a single parameter to a report's Parameters collection via the SQL query, the query looks like this:
SELECT * FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
WHERE Products.SupplierID = <%SupplierID|Enter Supplier ID|1|S|True|%>
You can also create a parameterized query from the Visual Query Designer. See Query Builder in Microsoft SQL Client and OLEDB Providers for further information on how to create a parameterized query using the interactive query designer.
There are six values in the parameter syntax, separated by the pipe character: |
Only the first value (Name) is required, but if you do not specify the third value (DefaultValue), the field list is not populated at design time. You can provide only the Name value and no pipes, or if you wish to provide some, but not all of the values, simply provide pipes with no space between them for the missing values. For example, <%ProductID||||False|%>
D (date) provides a drop-down calendar picker from which the user can select a date.
Depending on your data source, you may need to put number signs around the parameter syntax.
For example, #<%MyDateParameter%>#
When you add SQL parameters to a report, ActiveReports displays an 'Enter Report Parameters' dialog where the user can enter the values to fetch from the database. The steps to add a parameter to an SQL query are as follows:
Query |
Copy Code
|
---|---|
SELECT * FROM Products INNER JOIN (Orders INNER JOIN [Order Details] ON Orders.OrderID= [Order Details].OrderID) ON Products.ProductID = [Order Details].ProductID WHERE Products.SupplierID = <%SupplierID|Enter Supplier ID|7|||%> AND OrderDate >= #<%OrderDate|Order date from|11/1/1994|D|true|yyyy/MM/dd%># AND Discontinued = <%Discontinued|Is this checked?|true|B||%> |
The SQL query above causes ActiveReports to display the following dialog to the user. The user can accept these or input other values to select report data.
The steps to add a parameter from Report Explorer are as follows:
You can add, edit, and delete parameters at runtime. The following code demonstrates how to add a parameter and display its value in a Textbox control.
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste at beginning of code view. |
Copy Code
|
---|---|
Imports GrapeCity.ActiveReports.SectionReportModel |
Visual Basic.NET code. Paste INSIDE the ReportStart event. |
Copy Code
|
---|---|
Dim myParam1 As New Parameter() |
To write the code in C#
C# code. Paste at beginning of code view. |
Copy Code
|
---|---|
using GrapeCity.ActiveReports.SectionReportModel; |
C# code. Paste INSIDE the ReportStart event. |
Copy Code
|
---|---|
Parameter myParam1 = new Parameter(); |
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the FetchData event. |
Copy Code
|
---|---|
'Set textbox text equal to the value of the parameter. Me.txtParam1.Text = Me.Parameters("myParam1").Value |
To write the code in C#
C# code. Paste INSIDE the FetchData event. |
Copy Code
|
---|---|
//Set textbox text equal to the value of the parameter. this.txtParam1.Text = this.Parameters["myParam1"].Value; |
In order to prompt the user for parameter values, all of the following must be in place:
When there are parameters in the collection and the ShowParameterUI property is set to True, the user prompt automatically displays when the report is run. When the user enters the requested values and clicks the OK button, the report gets displayed using the specified values.
Values of a parameter added through the Report Explorer can be applied to a parameter in the SQL query by specifying the param:
prefix for a parameter in the SQL query. This prefix relates the current parameter to the one in the Report Explorer.
For e.g., SELECT * FROM CUSTOMERS WHERE CustomerName = '<%param:Parameter1%>'. In this case, the parameter with the param:
prefix in the SQL query is updated with values of the corresponding parameter in the Report Explorer.
Note: Within the same report, you can prompt users for some parameters and not for others by setting the PromptUser property to True for some and False for others. However, if the report object's ShowParameterUI property is set to False, the user prompt does not display any parameters, regardless of its PromptUser setting.