Configuring Parameter Persistence
In this section, you configure persistence (in a Web-based tutorial) for the parameter field selections that are made from the ListBox control.
To demonstrate lack of persistence for parameter selections
From the Build menu, select Build Solution.
If you have any build errors, go ahead and fix them now.
From the Debug menu, click Start.
In the ListBox control, SHIFT-click to select all cities in the list.
Click Redisplay Report.
The page reloads, and then displays the customer records for all customers in all cities. This is a large report that contains many pages.
- On the CrystalReportViewer toolbar, click Next Page.
The list of selected cities is not persisted. Page 2 of the report is not displayed. Instead, you see the startup parameter settings again (Paris and Tokyo).
- Return to Visual Studio and click Stop to exit from debug mode.
To add persistence code to the ConfigureCrystalReports() method
You must add persistence code to your application so that changes made are persisted when Web pages are reloaded.
To begin, you add persistence code to the ConfigureCrystalReports() method, by adding an Else block to the If Not IsPostBack conditional block. You then set unique values for the ArrayList instance for either condition in the conditional block. On page startup, you set the default values ("Paris" and "Tokyo") into the ArrayList instance. On page reloads, you retrieve the ArrayList instance that is stored in Session.
- In the ConfigureCrystalReports() method, cut and paste the two lines of code, which add Paris and Tokyo to the ArrayList, into the bottom of the Not IsPostBack conditional block.
When you are finished, the conditional block should look like this:
``` vb
If Not IsPostBack Then
defaultParameterValuesList.DataSource =
GetDefaultValuesFromParameterField(myParameterFields)
defaultParameterValuesList.DataBind()
myArrayList.Add("Paris")
myArrayList.Add("Tokyo")
End If
```
``` csharp
if (!IsPostBack)
{
defaultParameterValuesList.DataSource =
GetDefaultValuesFromParameterField(parameterFields);
defaultParameterValuesList.DataBind();
arrayList.Add("Paris");
arrayList.Add("Tokyo");
}
```
- Add a final line of code to the conditional block that assigns the ArrayList instance to Session.
> [!NOTE]
> <P>You can use the variable name as the string identifier for the Session that you add.</P>
``` vb
Session("myArrayList") = myArrayList
```
``` csharp
Session["arrayList"] = arrayList;
```
Add an Else condition to the Not IsPostBack conditional block.
Within the Else block, retrieve the ArrayList instance from Session and cast it to ArrayList.
myArrayList = Ctype(Session("myArrayList"), ArrayList)
arrayList = (ArrayList)Session["arrayList"];
When you are finished, the conditional block should look like this:
If Not IsPostBack Then
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(myParameterFields)
defaultParameterValuesList.DataBind()
myArrayList.Add("Paris")
myArrayList.Add("Tokyo")
Session("myArrayList") = myArrayList
Else
myArrayList = Ctype(Session("myArrayList"), ArrayList)
End If
if (!IsPostBack)
{
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(parameterFields);
defaultParameterValuesList.DataBind();
arrayList.Add("Paris");
arrayList.Add("Tokyo");
Session["arrayList"] = arrayList;
}
else
{
arrayList = (ArrayList)Session["arrayList"];
}
Those modifications to ConfigureCrystalReports() method ensure that the current instance of ArrayList is always available to be passed into the SetCurrentValuesForParameterField() method.
In the next section, you make two changes to the code in the button click event:
- Take the ArrayList that you created and assign it to Session.
- Replace the last two lines of code (that configure and display the report) with a call to the ConfigureCrystalReports() method to perform this functionality on a common set of code.
To modify the code in the button click event method to work with Session persistence
Delete the last three lines of code in the button click event method.
- The first line of code to delete is the code that binds the report file directory path to the ReportSource property of the CrystalReportViewer control.
- The second line of code to delete is the code that retrieves the ParameterFields instance from the ParameterFieldInfo property of the CrystalReportViewer control.
- The third line of code to delete is the call to the SetCurrentValuesForParameterField() method.
In the next step, you add two new lines of code to replace the deleted code.
Within the button click event method, immediately outside the foreach loop, add a line of code that assigns the ArrayList instance to Session.
Note
You can use the variable name as the string identifier for the Session that you add.
Session("myArrayList") = myArrayList
Session["arrayList"] = arrayList;
Call the ConfigureCrystalReports() method.
This retrieves the ArrayList instance, applies it to the report, and binds the report to the CrystalReportViewer control.
``` vb
ConfigureCrystalReports()
```
``` csharp
ConfigureCrystalReports();
```
To test the population of the defaultParameterValuesList ListBox control
You are now ready to build and run the project, to verify that the parameter field has been reset successfully.
From the Build menu, select Build Solution.
If you have any build errors, go ahead and fix them now.
From the Debug menu, click Start.
In the ListBox control, SHIFT-click to select all cities in the list.
Click Redisplay Report.
The page reloads and displays the customer records for all customers in all cities. This is a large report that contains many pages.
On the CrystalReportViewer toolbar, click Next Page.
The list of selected cities is now persisted. Page 2 of the report is displayed.
Return to Visual Studio and click Stop to exit from debug mode.