Crystal Reports is a terrific tool that allows you to easily
incorporate professional style reporting in your applications. The Crystal
Reports designer is easy to use and the Report Viewer Control makes it easy to
display reports. It also allows users to navigate through the report, export it
to a file, jump to specific groups, and even search the report. But, for as
long as I've been developing applications and especially reports, users always
seem to want more. A common request that I get is to run a report
automatically and have it emailed on a scheduled basis.
This article will show you how to do just that. I'll create
a Windows Service using Visual Studio 2008 and C#. I'll create a database to
store which reports need to be sent, to who, and when. For this sample I'm
going to use the Invoice report that was created in "Building
an Invoice Application with ASP.NET and Crystal Reports - Part 1". It
would be helpful for you to have read this article first but it is not
necessary. All the code for this article can be found here.
Before you begin, you will need to have installed Visual
Studio 2008 with Crystal Reports for .NET. The samples are written in
Visual Studio 2008 but they will work with Visual Studio 2005. You also
need to download the AdventureWorks sample database from http://msftdbprodsamples.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34032
for SQL Server 2008. Download and install the
SQL2008.AdventureWorks_All_Databases.x86.msi file. If you do not have SQL
Server 2008 you can use SQL Server 2005 but you'll need to download the 2005
AdventureWorks samples. You will use the AdventureWorks database to build
this report.
The Invoice report must be downloaded for this sample to
work. This is the Crystal Report report file that was created in the previous
article. You can download it here. Download
the zip file and unzip the Invoice.rpt file. Make sure to remember where you
saved the Invoice.rpt file because you'll set this path in the app.config file
later in this article.