Export (0) Print
Expand All

Creating a Report with a Selection Formula

Visual Studio 2005

In this section, you create a report that draws its information from the sample Microsoft Access database that ships with Crystal Reports.

To create a report with secure data from the Northwind database

Note   This procedure works only with a project that has been created from Project Setup. Project Setup contains specific namespace references and code configuration that is required for this procedure, and you will be unable to complete the procedure without that configuration. Therefore, before you begin this procedure, you must first follow the steps in Project Setup.
  1. In Solution Explorer, right-click the project name that is in bold type, point to Add, and then click Add New Item.
  2. In the Add New Item dialog box, in the Templates view, select the Crystal Report template.
  3. In the Name field, enter the name "CustomersBySalesName.rpt" and click Add.
    Note   In Visual Studio .NET 2002 or 2003, the button is named Open.
    If you have not registered before, you are asked to register. To find out how to register, see Crystal Reports Registration and Keycode.
  4. In the Create New Crystal Report Document panel of the Crystal Reports Gallery dialog box, select Using a Report Wizard.
  5. In the Choose an Expert panel, select Standard, and then click OK.
  6. In the Available Data Sources panel of the Standard Report Creation Wizard window, expand the Create New Connection folder.
    Note   In Visual Studio .NET 2002 or 2003 where Crystal Reports has not been upgraded to the full version, the Create New Connection folder does not exist; the contents are shown at the top level.
  7. From the subfolder that opens, expand the ODBC (RDO) folder.
  8. In the ODBC (RDO) window, select the correct ODBC DSN entry for your version of Crystal Reports as explained in ODBC DSN Entry for Xtreme Sample Database, and then click Finish.

    The ODBC (RDO) folder expands and shows the Xtreme Sample Database.

  9. Expand the Tables node, and then double-click the Customer table to move the table to the Selected Tables panel, and then click Next.
  10. Expand the Customer table, and then CTRL-click Customer Name, and Last Year's Sales.
  11. Click the > symbol to move these fields into the Fields to Display panel, and then click Next.
  12. In the Available Fields panel, under Report Fields, double-click Customer.Customer Name to move the field into the Group By panel, and then click Finish.

    The CustomersBySalesName report is created and loaded into the main window of Visual Studio.

Next, you create a selection formula to filter the data based on the Last Year's Sales field.

To create a selection formula based on Last Year's Sales

  1. Open the Web or Windows Form.
  2. From the View menu, click Code to view the code-behind class for this Web or Windows Form.
  3. For a Web Site, within the ConfigureCrystalReports() method (that you have created in Project Setup), create a Not IsPostBack conditional block.
    [Visual Basic]
    If Not IsPostBack Then
    
    End If
    [C#]
    if(!IsPostBack)
    {
    }
    Note   The Not IsPostBack conditional block encapsulates code that should be run only the first time the page loads.
  4. For a Web Site, add the following lines of code within the Not IsPostBack conditional block.

    For a Windows project, add the code to the ConfigureCrystalReports() method, without the Not IsPostBack conditional block.

    1. Enter the formula to select only records with Last Year's Sales greater than $11000.00, and customer names that begin with the letter "A".
      [Visual Basic]
      Dim mySelectFormula As String = "{Customer.Last Year's Sales} > 11000.00 " _
      & "AND Mid({Customer.Customer Name}, 1, 1) = ""A"" "
      [C#]
      string selectFormula = "{Customer.Last Year's Sales} > 11000.00 "
      + "AND Mid({Customer.Customer Name}, 1, 1) = \"A\"";
    2. Assign the selection formula string to the SelectionFormula property of the CrystalReportViewer control.
      [Visual Basic]
      myCrystalReportViewer.SelectionFormula = mySelectFormula
      [C#]
      crystalReportViewer.SelectionFormula = selectFormula;

Continue to Binding the Report.

See Also

Tutorials: Filtering Data Using Selection Formulas | Tutorials and Sample Code | Tutorials' Sample Code Directory

Show:
© 2014 Microsoft