Reporting in ASP.
NET
Crystal Reports 9.x
Challenges and Solutions
The 1st in a Series of Presentations on
Reporting with .NET
Don Robins
Outformations, Inc.
(c)2004 Outformations, Inc. 1
Agenda
Introductory Info (10)
Reporting Requirements (15)
Reporting Challenges (15)
Reporting Solutions (25)
BREAK (15)
Architecture & Code Review (40)
Traps To Avoid (5)
Summary (5)
Q/A (15)
(c)2004 Outformations, Inc. 2
[Link]
Don Robins - dbr@[Link]
– since 1985
– custom business applications and solutions for desktop
and (more recently) the web.
– BAADD member since 1988, current VP and [Link]
SIG leader
Outformations, Inc. – [Link]
– Collaborative design & software development practice
– Use Case based analysis and design
– Rational Unified Process (RUP) Project Management
– Collaboration tools and Project Workbook repositories
– We develop in Microsoft, Java, Open Source technologies
(c)2004 Outformations, Inc. 3
[Link]
Roles
– systems analyst, lead developer, lead technical architect,
application framework integration, database designer,
project management
Tech Skills
– [Link], HTML/Javascript, [Link]/C#, [Link],
ColdFusion, Lotus Notes, SQL7/2000, Access97/200x,
Office, FoxPro 2.x-VFP, Crystal Reports, OLE Automation
Industries
– biotech, aeronautics, transportation, manufacturing,
human resources, import/export, medical,
pharmaceutical, legal, broadcasting, entertainment,
labor relations, land trusts, retail, real estate, financial,
publishing, technology and education
(c)2004 Outformations, Inc. 4
[Link]
Reporting with xBase, FoxPro 2.x through VFP6
– Early report frameworks and templates
– Early ad-hoc reporting engines
– Early FoxFire! prototyping, development and beta tester
Reporting with Access 2.0, 95, 97-200x
– Desktop applications and ODBC reporting
Reporting with Dynamic HTML based reporting
– ColdFusion and ASP Classic dynamically generated HTML
Reporting with Lotus Notes
– Notes ‘View’ based reporting
(c)2004 Outformations, Inc. 5
[Link]
Share challenges faced
– Identify some complex reporting requirements
– Identify where leverage is needed with Crystal
Share successes reached
– Share ideas of how to implement solutions
– Identify some best practices to leverage Crystal
Share my FMR development philosophy
– Functionality
– Maintainability
– Robustness
(c)2004 Outformations, Inc. 6
[Link]
We will spend time to set high level context
before examining code and architecture.
My choices were made by trial and error.
My solutions may not work for you in your
situation(s).
Most issues apply regardless of platform, but no
feasibility has been performed other than in .NET
An interactive discussion of alternatives is
welcomed during Q/A after presentation or in
email.
(c)2004 Outformations, Inc. 7
[Link]
Application Statistics:
Application – [Link] Database - SQL Server 2K
150+ Pages/Forms 75+ Tables in 2 DBs
150+ .NET User Controls 1000+ Fields
50+ Business Objects 500+ Stored Procedures
250+ Users in 50+ Offices 60+ SQL UDFs
Deployment Statistics:
Application Servers Database Servers
3 Development Servers 3 Development Servers
1 Staging Server 1 Staging Server
1 Production Server 1 Production DB Server
(c)2004 Outformations, Inc. 8
[Link]
3 Primary Reports: (see samples)
– Relatively simple narrative document
– ‘Term Sheet’ - 2 to 5 pages
– Fairly consistent layout
– Simple narrative + data
– Extremely complex narrative document
– ‘Fact Sheet’ – 10 to 60 pages
– Multiple sections and inconsistent layout and data
– Complex narrative + data
– Simple one page form style report
– ‘Faxlet’ - 1 page
– Fairly consistent form layout
– Simple narrative + data
(c)2004 Outformations, Inc. 9
[Link]
1. Always launched by the user from within the
context of one parent entity.
2. Document style report generated as PDF or
Word DOC.
3. No Crystal viewer, no drill-down.
4. Must be generated, downloaded and opened in
the associated application on the desktop with
one mouse click.
(c)2004 Outformations, Inc. 10
[Link]
5. Must allow user to save download as a local
file.
6. Report must reflect current state of the data,
no automated batching.
7. Reports need to be launched in the
background during workflow processes,
captured and stored in the database for
retrieval on demand by users.
8. Reports must integrate with the complex
application role based, data level security.
(c)2004 Outformations, Inc. 11
[Link]-Functional
1. Application MUST use Crystal Reports
– In house developer and user knowledge of Crystal
– Already own licenses (next session)
– They understand it is ‘integrated’ with .NET
2. Output MUST emulate existing reports
– Database and application will support all data required
– No major changes in report look and feel (at inception)
– Must be highly maintainable and extendable
– Re-factoring is expected after rollout
(c)2004 Outformations, Inc. 12
[Link]
1. Must use the most flexible [Link] project
integration and IDE choice.
2. Must integrate with the application’s dynamic
data connectivity.
3. Must use the most maintainable, reusable and
robust data retrieval method.
4. Must support flexible and dynamic parameter
passing to reports and sub-reports.
(c)2004 Outformations, Inc. 13
[Link]
5. Must build large, diverse and complex reports
with varying data layouts and deliver with one
mouse click.
6. Must output to a variety of user selectable file
formats.
7. Must allow for automatic storage and user
retrieval from a row in a table.
8. Must secure the data from unauthorized users
by integrating with complex, role based,
metadata driven application security.
(c)2004 Outformations, Inc. 14
[Link]
THE CHALLENGES ARE NOW CLEAR.
WHAT ARE YOUR OPTIONS?
(c)2004 Outformations, Inc. 15
[Link](0)
OPTION #1
(c)2004 Outformations, Inc. 16
[Link](1)
OPTION #2
Figure it out…
Divide & Conquer
Leverage
Re-factor
(c)2004 Outformations, Inc. 17
[Link](0)
[Link] Integration and IDE
Visual Studio vs. Crystal 9 Studio
– VS .NET V1.0 integrated with Crystal v.8.5
– Crippled functionality from within Visual Studio
– Buy a developer license for Crystal 9
Embed RPT files in Project and bind to DLL?
– Pros and cons of embedding
– Extended reporting hooks in code behind BUT…
– Modification requires recompilation
– Drop RPT files into specified folder
– Don’t bind! No recompilation required
(c)2004 Outformations, Inc. 18
[Link](1)
Dynamic Data Connectivity
Use connections in studio at design time
– Maintain multiple connections in IDE
– Connect as needed at design time
– Save data and refresh connections at will.
Reset report object connections at runtime
– Create library of generic reporting services
– Pass report object and connection data
– Connection method iterates all master and sub-
reports resetting connections prior to generation
(c)2004 Outformations, Inc. 19
[Link](1)
Dynamic Data Connectivity Example
using [Link];
using [Link];
//Set Connection Info…
[Link] = GetNamedItem([Link], "User ID", "=", ";");
[Link] = GetNamedItem([Link], "Password", "=", ";");
[Link] = GetNamedItem([Link], "Data Source", "=", ";");
[Link] = GetNamedItem([Link], "Initial Catalog", "=", ";");
TableLogOnInfos crTableLogonInfos = new TableLogOnInfos();
TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
//Loop through each table and apply connection info...
foreach([Link] t in [Link])
{
[Link] = MyConn;
[Link](crTableLogonInfo);
[Link] = [Link];
[Link](crTableLogonInfo);
}
(c)2004 Outformations, Inc. 20
[Link](2)
Maintainable Robust Data Retrieval
Use stored procedures and SQL functions
– Avoid embedding SQL in report objects
– Create and test all required row-sets using SQL
Analyzer or equivalent tools
– Allows complex and flexible data retrieval with
simple wiring to reports
– Allows decoupling of report design from data
programming
– Allows reusability of SQL objects for like reports and
sub-reports
– Use intelligent naming conventions
(c)2004 Outformations, Inc. 21
[Link](2)
Stored Procedure Data Source Example
Open [Link]
– Show how fields are bound to columns
Open usp_rptTermSheetMaster
– Review complex construction of rowset
Note the naming convention
– Provides intuitive mapping from report to data
source and vice versa
(c)2004 Outformations, Inc. 22
[Link](3)
Dynamic Parameter Passing
Pass parameters at runtime
– Set at design time, easy to test
– Parameters can be chained into sub-reports
– Determined at runtime based on context
– User entry dialogs can gather and forward
– Passed to generic reporting service methods
(c)2004 Outformations, Inc. 23
[Link](3)
Dynamic Parameter Passing Example
SetParm(
ref [Link] pReport,
string pParmName,
string pParmValue)
ParameterValues MyValues = new ParameterValues();
ParameterDiscreteValue MyValue = new ParameterDiscreteValue();
MyValues
=[Link][pParmName].CurrentValues;
[Link] = pParmValue;
[Link](MyValue);
[Link][pParmName].ApplyCurrentValues(
MyValues);
(c)2004 Outformations, Inc. 24
[Link](4)
Large, Diverse, Complex Reports
Chunk reports to largest practical objects
– Only one level of nesting for sub-reports
– Report output as PDF can be merged (stitched)
– Use third party server components wrapped in
services
– Launch report sections separately or chained,
allowing for diverse and hierarchical report ‘sections’
– Allows unlimited size and complexity
– May not work with Crystal viewers unless separate
reports
(c)2004 Outformations, Inc. 25
[Link](4)
PDF Merge Examples w/ 3rd Party Component
Imports [Link]
Imports [Link]
_AppendAll(ByVal pages As Pages, ByVal path As String)
'Bind the file specified in 'path' to the target document in 'pages'...
Dim fileStream As New FileStream(path, [Link], [Link])
Dim reader As New BinaryReader(fileStream)
Dim buffer As Byte() = [Link](CInt([Link]))
[Link]()
reader = New BinaryReader(New MemoryStream(buffer, False))
Dim [source] As New Document(reader)
Dim i As Integer
For i = 0 To [source].[Link] - 1
[Link]([source].Pages(i))
Next i
(c)2004 Outformations, Inc. 26
[Link](5)
Output Variety Of Formats
Crystal Output Types:
– Adobe PDF
– Word DOC
– Rich Text File RTF
– Excel XLS
Maximum Control For User Review:
– Conditionally set the output type prior to generation
– Dynamically adjust HTML meta header on redirect
– User is prompted with options to save or open
– Opens with appropriate application on desktop
(c)2004 Outformations, Inc. 27
[Link](5)
Output As PDF Example
ExportToPDF(ReportDocument oRD, string TargetFileName)
//Set up output configuration
DiskFileDestinationOptions _Options = new DiskFileDestinationOptions();
_Options.DiskFileName = TargetFileName;
//Set the configuration onto the report object...
[Link] =
[Link];
[Link] =
[Link];
[Link] = _Options;
[Link] =
[Link];
//Build to PDF in output folder for user as specified in the output filename...
[Link]();
(c)2004 Outformations, Inc. 28
[Link](5)
Redirection Example
RedirectPDFToBrowserStream(Page oPage, string OutputFileName, string
OutputPDFName)
//Send the file to the user that made the request
[Link]();
[Link] = true;
[Link]("Content-Type","application/pdf");
//Default the name that it would be saved with...
[Link]("Content-Disposition", "attachment;
filename=" + OutputPDFName + ";");
//Set up the stream for the output file...
FileStream Fs = new FileStream(OutputFileName,[Link]);
long FileSize = [Link];
byte[] bBuffer = new Byte[FileSize];
[Link](bBuffer,0,Convert.ToInt32(FileSize));
[Link]();
//Stream the file back...
[Link](bBuffer);
[Link]();
[Link]();
(c)2004 Outformations, Inc. 29
[Link](6)
Storage And Retrieval From Table
Convert binary output file to byte array
– Can be contained in SQL Server IMAGE data type
– Passed into and out of stored procedures
– Create a table with report row keyed to parent
– Allows content to be stored within a database
– No need to manage files on the server
– Scales across server farms
– Piggybacks on DB backup and maintenance
– Increases size of database
(c)2004 Outformations, Inc. 30
[Link](6)
Binary File Storage Example
CREATE PROCEDURE usp_ProjectReportInsert
--Purpose: Inserts a Project Report.
(
@user_id int= null
,@proj_id int= null
,@prrp_vch_type_alias varchar(50) = null
,@prrp_img_file image = null
)
(c)2004 Outformations, Inc. 31
[Link](7)
Integrate With System Security
Stored procedures retrieval leverages security
– Build joins as needed to support permission logic
– All complex logic maintained outside of report
objects
– If spoofed, report can launch but no data shows
– Report can contain business logic driven by meta
data passed in row-sets adjacent to content
(c)2004 Outformations, Inc. 32
[Link](7)
Integrate With System Security Example
SELECT ...
FROM ...
WHERE ...
AND
--Project Phase security REQUIRES that the
--logged user_id be linked to the project in the
-- tblproj_user_role_link table, derived by UDF()...
dbo.usf_proj_IsPermitted(@user_id, @proj_id, '') = 1
(c)2004 Outformations, Inc. 33
[Link](15)
BREAK – 15 min
(c)2004 Outformations, Inc. 34
[Link]
Demo the Reports:
1) Launch from Project summary page
2) Retrieve archived report
3) Review call sequence from top down
(c)2004 Outformations, Inc. 35
[Link]
Call From High Level UI Code Behind
Minimize logic in UI layer
– Instantiate wrapper class
Protected _report As New PTS_Report()
– Initialize report and call alternate launch methods
_report.InitReport(…)
_report.Show(…)
_report.BuildComposite(…)
_report.ShowComposite(…)
(Show code module [Link])
(c)2004 Outformations, Inc. 36
[Link]
Build a Mid Level Services Class
Contains reporting application specific services:
– SetupReport(…)
– ConfigureReport(…)
– SetReportParms(…)
– GenerateReport(…)
– DisplayReport(…)
– DisplayReportFromArray (…)
– StoreReport(…)
– AppendAll(…uses third party stitching component…)
(Show module PTS_Report.vb) * Note: [Link]
(c)2004 Outformations, Inc. 37
[Link]
Build a Low Level Services Class
Contains generic reporting related services:
– SetConnection(…)
– GetUserFolder(…)
– SetParm(…)
– ExportToPDF (…)
– ExportToDOC (…)
– RedirectPDFToBrowserStream (…)
– RedirectDOCToBrowserStream(…)
(Show module [Link]) * Note: C#
(c)2004 Outformations, Inc. 38
[Link]
Build Additional Calls Into a Utility Class
Manage auto launch and store services:
– PTS_System.BuildFactSheet(…)
– PTS_ [Link](…)
– PTS_ [Link](…)
Pass in arguments from calling methods:
– Entity keys
– Template names
– File paths
(Show code module PTS_System.vb)
(c)2004 Outformations, Inc. 39
[Link]
1. Allow completion of compile following a project build
prior to calling any report.
2. Use temp output files with embedded session id for
insuring distinct temp files to avoid user collisions.
3. Utilize user folders for automated management,
cleanup and auditing output errors.
4. Set and double check project references for desired
version of Crystal DLLs as multiple versions may exist
on the server or development workstation.
5. Minimize complexity within the report objects for
maximum flexibility and robustness.
(c)2004 Outformations, Inc. 40
[Link]
1. These solutions are not trivial – but they do work.
2. The application and reporting modules have been in
production since June 2003, and continue to be
periodically extended and re-factored.
3. These techniques should work across development
languages and databases.
4. Crystal just works and rises to the challenges IF you
manage the landmines to leverage an enormous amount
of functionality and flexibility.
5. The key to success is making development choices that
provide FMR – Functionality, Maintainability and
Robustness.
(c)2004 Outformations, Inc. 41
[Link]
1. Bay Area Crystal Decisions User Group
(BACDUG) holds meetings three to four times
per year in the San Francisco Bay area. The
meetings are open to all users of Crystal
Decision's products
[Link]
2. Crystal Decisions Developer Zone
[Link]/products/dev_zone
(c)2004 Outformations, Inc. 42
[Link]
Q&A
Don Robins
Outformations, Inc.
dbr@[Link]
(c)2004 Outformations, Inc. 43