AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1297&pId=-1
Displaying Crystal Reports using WinForms and C#
page
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 75973/ 143

Introduction

Recently I've received some emails asking me to show how Crystal Reports can be displayed in WinForms.  Specifically they were about showing the report without having the login dialog display.  Since most of the articles I've written deal with ASP.NET web applications, and the examples are for that, I thought this would be a good opportunity.

Windows Forms are just a little different than web applications.  Obviously keeping state for windows forms is much nicer than keeping state for an ASP.NET web application.  The downside, in my opinion is deployment for WinForms.  Each client needs to have the right .NET Framework installed and the Crystal components.  Web applications, rely on just installation at the server level. 

System Requirements

Visual Studio 2005

Crystal Reports XI

Creating a WinForm for the report

First let's create a project for the WinForm that will contain the report.  In the Visual Studio 2005 IDE, go to create New Project and select "Windows Application" or the "Crystal Reports Application".  For this example, select the Windows Application project.  We'll add an existing report to this application that we will display on the form.

Right click on the new project (called something like WindowsApplication1), and then add the burn down report from our other article. Do this by right clicking on your project, select Add, then click on Existing items.  Browse the directories and select the Burndown report to add to your project.

Next we want to add the starting windows form that will display the report.  You can use an existing one, or add one by right clicking on your project, clicking Add then new and selecting Windows Form.

In the form, we need to add the Crystal Viewer.  So open the toolbox window on the left side of the Visual Studio window.  Expand the Crystal Reports section, then drag and drop the Crystal Viewer control on the Form.  This is where we'll bind the burndown chart to.

To bind this, we simply right click on the Crystal Report Viewer (I'm assuming it's called CrystalReportViewer1).  In the Report Source select the Burndown report.  It should be prefaced with the namespace and class of your project.  Once this is bound, we can now walk through automatically logging onto your report.

Logging on using a Crystal Report Document

Let's see how we can log into the report automatically.  First we need to add a button that we will use to automatically log in using the Crystal Report Document object.  The next section we will use the log on setting option in the Crystal Viewer.

First, let's drag a button over on the form.  Drag over a button from the Toolbox, Windows forms.  It should look similar to Figure 1.

Figure 1

The above figure also contains the button called "Logon Viewer" which we will talk through in the next section.  In the following code

Listing 1

Burndown rptBurndown = new Burndown();
CrystalDecisions.Shared.ConnectionInfo crDbConnection = new
  CrystalDecisions.Shared.ConnectionInfo();
crDbConnection.IntegratedSecurity = true;
crDbConnection.DatabaseName = "TfsWorkItemTracking";
crDbConnection.ServerName = "tfdbserver";
CrystalDecisions.CrystalReports.Engine.Database crDatabase =
  rptBurndown.Database;
CrystalDecisions.Shared.TableLogOnInfo oCrTableLoginInfo;
foreach (CrystalDecisions.CrystalReports.Engine.Table oCrTable in
  crDatabase.Tables)
{
  oCrTableLoginInfo = oCrTable.LogOnInfo;
  oCrTableLoginInfo.ConnectionInfo = crDbConnection;
  oCrTable.ApplyLogOnInfo(oCrTableLoginInfo);
}
 
crystalReportViewer1.ReportSource = rptBurndown;      

In this code, we are able to log onto a report document.  First we create a new instance of the Burndown report.  We create a new ConnectionInfo object, for this instance just one will do.  If you would ever need to log onto different sources, then you need to make multiple connectioninfo objects.  We then get a Database object from the Burndown report object. 

Finally we loop through all the tables in the reports database.tables collection.  First getting TableLoginInfo objects from the report database, we set the connectioninfo property of that object to the database connection info set earlier.  Here is where you would need to make changes if you are logging into multiple data sources.

Logging on using the CrystalViewer

Now let's look into setting the connection info using the Crystal Viewer object.  This is great if you are always using the same report in the Crystal Viewer object.  If you want to use the Viewer to display more than one report, the Report Document method probably makes more sense to use.  See Listing 2 for the C# code that sets the connection information on CrystalViewer1.

Listing 2

CrystalDecisions.Shared.ConnectionInfo crDbConnection = new
  CrystalDecisions.Shared.ConnectionInfo();
crDbConnection.IntegratedSecurity = true;
crDbConnection.DatabaseName = "TfsWorkItemTracking";
crDbConnection.ServerName = "tfdbserver";
CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt =
  (CrystalDecisions.CrystalReports.Engine.ReportDocument)
  crystalReportViewer1.ReportSource;
CrystalDecisions.CrystalReports.Engine.Database crDatabase = oRpt.Database;
CrystalDecisions.Shared.TableLogOnInfo oCrTableLoginInfo;
foreach (CrystalDecisions.CrystalReports.Engine.Table oCrTable in
  crDatabase.Tables)
{
  oCrTableLoginInfo = oCrTable.LogOnInfo;
  oCrTableLoginInfo.ConnectionInfo = crDbConnection;
  oCrTable.ApplyLogOnInfo(oCrTableLoginInfo);
}            

As we can see, the connection information setting is pretty much the same.  We actually do use a Report Document object to help in setting the database.  We do this by Casting the report viewer reportsource to the ReportDocument object.  Once that is done, everything else is similar to the last section.

Downloads
Summary

Now we've thoroughly delved into setting connection information using a Windows Form.  As mentioned the two methods shown here are similar but can be used in different circumstances.  Continue making great reports, and happy coding!



©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-06-25 7:58:05 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search