Share via


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

  1. From the Build menu, select Build Solution.

  2. If you have any build errors, go ahead and fix them now.

  3. From the Debug menu, click Start.

  4. In the ListBox control, SHIFT-click to select all cities in the list.

  5. 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.
  1. 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).
  1. 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.

  1. 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");
}
```
  1. 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;
```
  1. Add an Else condition to the Not IsPostBack conditional block.

  2. 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

  1. 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.
  1. 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;
    
  2. 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.

  1. From the Build menu, select Build Solution.

  2. If you have any build errors, go ahead and fix them now.

  3. From the Debug menu, click Start.

  4. In the ListBox control, SHIFT-click to select all cities in the list.

  5. 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.
  1. On the CrystalReportViewer toolbar, click Next Page.

  2. The list of selected cities is now persisted. Page 2 of the report is displayed.

  3. Return to Visual Studio and click Stop to exit from debug mode.