Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Crystal Report Filtering Using Selection Parameters

0.00/5 (No votes)
2 Jun 2011 2  
Beginner's Guide to Crystal Report Filtering

Background

Selection parameters allow you to filter Data returned from datasource (Table in our case).

Of course, it's better and faster to use Stored Procedures (explained here), but it's good to learn about this useful feature.

Step 1: Database

  1. Download and unzip the attached file.
  2. Run the script to create the database and table.
  3. You should now have the following schema.

Schema.jpg

(I tried to use variations of data types, text, datetime, float, etc.)

Step 2: Form

We’ll build the user input form, nothing fancy, textbox, datetimepicker and a customized numeric updown control.

(The numeric updown is augmented to have a Checked property, we’ll need that later.)

Main.jpg

Step 3: Report

Next, we’ll add a new report to our Reports folder, explained here.

Step 4: Report’s Parameters

Very well, we have our report created; next, we want to create the parameters to be passed and searched with.

In addition to the 6 parameters on the user form, we need a Boolean parameter for each field, this Boolean tells us whether to include the field in search criteria or not.

We'll enter the first boolean to indicate if the From datetimepicker has value:

  1. In field explorer,right click Parameter fields>New (if you can't find field explorer, View>Other Windows>Document Outline or Ctrl+Alt+T)

    Parameter_1.jpg

  2. Enter name bHireDateFromChecked and type boolean

    All_params.jpg

  3. Do this for the other fields. bHireDateToChecked bSalaryFromChecked etc…

We don’t need a Boolean to check if user wants to search by name, we’ll just inspect if the text length is greater than zero.

So now we’ve added the Booleans. Next, we’ll add the parameters that will be actually searched.

Similarly add the parameters, with the same datatype they are in database.

So now we have:

SelectDatabase.jpg

Step 5: Report’s Formulas

For each of our Main searchable parameters, we’ll create a formula, this formula examines the parameters attached Boolean, to decide whether to include it in the search or not.

So let’s add a new formula:

  1. Open Field Explorer
  2. Right click Formula Fields>New
  3. Enter name: HireDateFrom

After you click ok,this window appears:

SelectTable.jpg

You have all the parameters you created, if you double click on one of them, it's written into the editor. Great!

So now we’ll write the following formula while clicking parameters of the above panel.

if (double click bHireDateFromChecked) then 
double click t_Employees.dHireDate >= double click dtHireFrom 
Else 
True  

So now you should have the following:

IF({?bHireDateFromChecked}) THEN
    {t_Employees.dHireDate}>={?dtHireFrom}
ELSE
    TRUE 

Click save and close on the top left.

Similarly, create the formula HireDateTo, which is:

IF({?bHireDateToChecked})THEN
    {t_Employees.dHireDate}<={?dtHireTo}
ELSE
    TRUE

Similarly create another 2 formulas for salary, from and to.

For the name field, we’ll take a different approach, we’ll check the passed parameter’s length.

(Notice the functions panel in the middle, like the formulas, functions are written into the editor once you double click on one of them.)

Expand Strings in the Functions Panel.

Write:

IF(double click length double click sName)>0)THEN
double click t_Employees.sName = double click sName 
ELSE
TRUE 

And now you should have:

IF(Length ({?sName})>0)THEN
    {t_Employees.sName}={?sName}
ELSE
    TRUE

Step 6: Selection Parameters

Right click in the empty grey of the report>Report>selection formula>record.

Like the parameters, the formulas we just created are available in the editor.

FieldExplorer.jpg

Now all we have to do is to join these formulas using the logical operator AND.

  1. Double click on each formula to be written in the editor
  2. Write AND between each formula

..and you should end up with this:

{@HireDateFrom} AND
{@HireDateTo} AND
{@Manager} AND
{@Name} AND
{@SalaryFrom} AND
{@SalaryTo}

Points of Interest

You design the report in one place, then copy it somewhere else.

The report included in the application (the one you see under solution explorer->Reports Folder) is NOT the one the report viewer references to, the actual path is this:

string strReportName = "rptDemo.rpt";
string strPath = Application.StartupPath + "\\Reports\\" + strReportName;

which is the application's bin>Debug WHY?

In a Desktop application, when you want to deploy and add a setup project, this will be the path of the added Report's Folder.

Deploy.jpg

You should NOT hard-code your Connection String.

Almost every CR tutorial I've seen uses hard-coded Connection Strings (they use Datatables too, someone tell me why?)

You should make use of little helpful class called DbConnectionInfo (not mine, unfortunately I can't remember the author to give him credit.)

private void frmReportViewer_Load(object sender, EventArgs e)
        {
            DbConnectionInfo.SetConnectionString(ConfigurationSettings.AppSettings[0]);
            TableLogOnInfo logOnInfo;
            ConnectionInfo connectionInfo;
            foreach (Table table in m_cryRpt.Database.Tables)
            {
                logOnInfo = table.LogOnInfo;
                connectionInfo = logOnInfo.ConnectionInfo;
                // Set the Connection parameters.
                connectionInfo.DatabaseName = DbConnectionInfo.InitialCatalog;
                connectionInfo.ServerName = DbConnectionInfo.ServerName;
                if (!DbConnectionInfo.UseIntegratedSecurity)
                {
                    connectionInfo.Password = DbConnectionInfo.Password;
                    connectionInfo.UserID = DbConnectionInfo.UserName;
                }
                else
                {
                    connectionInfo.IntegratedSecurity = true;
                }
                table.ApplyLogOnInfo(logOnInfo);
            }

            crystalReportViewer1.ReportSource = m_cryRpt;
            crystalReportViewer1.Refresh();
        }

You should let the user know what he entered.

ReportHeader.jpg

Always display the parameters the user entered in a friendly way.

For example, the formula lblHireDateFrom displays the entered From date:

IF({?bHireDateFromChecked}) THEN
   ToText ({?dtHireFrom})
ELSE
    'Any'

Conclusion

Hope I delivered a clear explanation. Feel free to post any questions/suggestions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here