Introduction
The company I own provides custom software solutions to Fortune 500 companies in Los Angeles, CA and Miami, FL for C# and VB .NET WinForm and WebForm applications. We can provide a Web or WinForm app that would take 6 programmers a year to complete in just DAYS thereby saving our clients, in some cases, millions of dollars.
To be able to do this, I have written what I call "Model Applications" of MDI & SDI WinForms, and Web Apps, etc. that simply require changing artwork and business logic.
I use Crystal Reports in my model apps for creating reports. This is the second part of a 2 part article, that will demonstrate how to implement a generic solution to using Crystal Reports in Web Apps.
Part I of this article dealt with WinForms. You can read Part I here.
This article, Part II, deals with Web Apps.
Background
Crystal Reports is not easy to work with, and it has plenty of bugs and quirks in my experience. But the big companies all seem to like it so I had to develop a generic approach to using it.
One of the worst features of Crystal Reports is the way in which it handles report Parameters by presenting each parameter in a separate screen which clients really hate! To solve this, this demo will show you how to present ALL of the parameters contained in a *.rpt file in a SINGLE screen to the user.
This demo displays reports using two Web pages, i.e. Reports.aspx, and a Params.aspx. You could combine these Web pages which is easy to do. These screens contain the following:
- Reports.aspx: This Web page has a pseudo-comboBox that displays a list of reports that the user can select from. When the user presses the "Get Report" button, a modal window called the Parameters Viewer will be launched.
- Params.aspx: This Web page displays the parameters contained in the report selected in a
datagrid
that we dynamically add controls to in order to make selecting parameter values easier for the user. After setting the parameter values for the selected report, the user will close the Parameters Viewer and the Reports Viewer will display the selected report with the selected parameters.
Crystal Reports create a binary file with a "RPT" extension that contains ALL of the information we need to display any report, and so that is what we will use in this generic solution.
- Select the *.rpt file
- Read the parameter information directly from the *.rpt file
- Build a table dynamically displaying all of the parameters in the selected *.rpt report file.
- The parameter table that we create dynamically must contain controls that are also created dynamically to facilitate the users selection of parameters like a date calendar, checkboxes, etc.
- We must create validation controls dynamically from the parameter data that we read from the *.rpt file.
- Finally, we pass the parameter data into the Crystal Viewer and display our selected Crystal Report.
- Database connection information is read dynamically from the selected *.rpt file.
In the demo reports included, I didn't bother to connect to any data source since doing that is trivial and the main point of this article is to show how to display the parameter data in a datagrid with selection controls dynamically. But all of the code that does this is in place in the demo and will read the database information dynamically if there is any database information in the *.rpt file.
Web.config Settings
If you have a problem running this sample Web app, check your Web.config file and make sure that the <assemblies></assemblies>
section is EMPTY! Visual Studio puts the version of the Crystal Reports DLLs into the assemblies section AUTOMATICALLY and if you don't have the exact same version numbers you will get an error message! Your <assemblies>
section should be empty as follows:
<compilation defaultLanguage="c#" debug="true"><assemblies></assemblies></compilation>
Reading Parameter Data from *.rpt Report Files
We started by pre-defining the columns of our Datagrid
of Parameters as follows: Parameter
, Kind
, Value
, Min
, Max
, Prompt
.
Add we will add each parameter kind as a new row to our datagrid
.
BooleanParameter
CurrencyParameter
DateParameter
DateTimeParameter
NumberParameter
StringParameter
TimeParameter
To dynamically build a datagrid
of parameters, we read parameters from the *.rpt file as follows:
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions crParamFieldDefinitions
= crDoc.DataDefinition.ParameterFields;
foreach (CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition def
in crParamFieldDefinitions)
For each parameter in the foreach
loop, we add a row to our dynamic datagrid
which has pre-defined columns.
Dynamically Adding Controls to Our Datagrid of Parameters
What is unusual about our datagrid
of parameters is that we are adding each parameter as a new row instead of as a new column which is what is more commonly done in datagrid
s. This means that I needed to implement a different approach to adding user controls. To accomplish this, I used the GotFocus
event of the datagrid
to selectively add/remove controls based upon the Parameter Kind listed in column 2 of the datagrid
.
For the purposes of this article, I only implemented three controls types:
DateTime
TimePicker
- Radio Buttons(or
CheckBox
)
For example, when a user clicks on the datagrid
, if column 2 has a value of DateTime
, then a DateTime
control will appear in the data cell for the user to select a DateTime
. I should point out that some programmers prefer using a "Radio Button" for boolean values but I prefer using a simple CheckBox
for boolean values which I implemented in the demo here. It should be obvious that you can implement ANY controls that turn you on or that a client requests.
The DateTime Picker Control
The DateTime
picker is a control that allows the user to select either a date, a datetime
and if I get some extra time I will add a time spinner to it. The source code for this control is included here.
Points of Interest
File Exports
I included the ability to export the loaded report in a number of various file types including:
- Rich Text Document (RTF)
- Portable Document (PDF)
- Microsoft Word (DOC)
- Microsoft Excel (XLS)
- Crystal Report (RPT)
- HTML 3.2 (HTML)
- HTML 4.0 (HTML)
Uranus File
I eliminated my infamous "uranus" file. Uranus is typically a dark, gaseous and unpleasant planet.
Project Pathways
Load ALL of the Crystal Report DLLs which can be found at C:\Program Files\Common Files\Crystal Decisions\1.1\Managed.
That's all folks!
If you have any questions about this article or source code, please feel free to contact me.
Contact Details