Share via


Setting the Selection Formula Manually in Code

You are now ready to add code to modify the selection formula in the code-behind class.

To code the selection formula

  1. Open the Web or Windows Form.

  2. From the View menu, select Designer.

  3. Double-click Redisplay Report.

    The code-behind class for the report appears and shows that a redisplay_Click() event method has been automatically generated.

  4. For the selection formula, create a string variable that takes the values from the TextBox controls.

The selection formula is similar to the text you have typed within the ConfigureCrystalReports() method. Instead of a minimum sales value of $11000.00, you use the value from the lastYearsSales TextBox control. For the Customer Name field, you use the value from the customerName TextBox control.

``` vb
Dim mySelectFormula As String = "{Customer.Last Year's Sales} >
" & lastYearsSales.Text _
& " AND Mid({Customer.Customer Name}, 1) > """ &
customerName.Text & """"
```

``` csharp
string selectFormula = "{Customer.Last Year's Sales} > " +
lastYearsSales.Text
+ " AND Mid({Customer.Customer Name}, 1) > \"" +
customerName.Text + "\"";
```
  1. Assign the string variable to the SelectionFormula property of the CrystalReportViewer control.

    myCrystalReportViewer.SelectionFormula = mySelectFormula
    
    crystalReportViewer.SelectionFormula = selectFormula;
    
  2. Rebind the CustomerBySalesName report to the ReportSource property of the CrystalReportViewer control.

    Note

    The file directory path that is shown here is for a Visual Studio project. ProjectName is replaced by the name of your Web or Windows project. UserName is replaced by your computer logon name.

    • The default path for a Web Site project is as follows:

      myCrystalReportViewer.ReportSource = "C:\WebSites\
      ProjectName\CustomersBySalesName.rpt"
      
      crystalReportViewer.ReportSource = "C:\\WebSites\\
      ProjectName\\CustomersBySalesName.rpt";
      
    • The default path for a Windows project is as follows:

      myCrystalReportViewer.ReportSource = "C:\Documents and Settings\
      UserName\My Documents\Visual Studio\Projects\
      ProjectName\CustomersBySalesName.rpt"
      
      crystalReportViewer.ReportSource = "C:\\Documents and Settings\\
      UserName\\My Documents\\Visual Studio\\Projects\\
       ProjectName\\CustomersBySalesName.rpt";
      

You have now created a selection formula that you can modify at run time.

To test the selection formula

  1. From the Build menu, click 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 lastYearsSalesTextBox, type 200000.

  5. In the customerNameTextBox, type SAB

  6. Click RedisplayReport.

The Crystal report displays three customer records: SAB Mountain, Tek Bikes, and Tienda de Bicicletas El Pardo.

Only customer records with names greater than "SAB" and last year's sales greater than "200000" are displayed.
  1. Return to Visual Studio and click Stop to exit from debug mode.