1.6. Using the Crystal Reports Viewer

Now that you have a report, you need to allow a user to view it. So far, you've seen how to preview the report in the Visual Studio Designer, but the user of your application needs a way to view the report. Crystal Reports comes with a viewer control that will do all the heavy lifting, and it is a relatively trivial task to display your report to the user. Follow these steps to change the Default page to display the Employee report you've just created.

  1. Open the Default.aspx page in Design mode.

  2. From the toolbar, drag a CrystalReportsViewer control onto the Default.aspx page.

  3. Double-click on the Default page to create the Page_Load event in the code-behind page, and enter the following code:

    protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument report = new ReportDocument();
        report.Load(Server.MapPath("Employee.rpt"));
    
        SetTableLocation(report.Database.Tables);
    
        CrystalReportViewer1.ReportSource = report;
    }

  4. Add the following using directives to the beginning of the page class:

    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;

  5. The Page_Load event procedure calls a custom method called SetTableLocation. Enter the following code after the Page_Load event:

    private void SetTableLocation(Tables tables)
    {
        ConnectionInfo connectionInfo = new ConnectionInfo();
    
        connectionInfo.ServerName = "VARALLOV01";
        connectionInfo.DatabaseName = "AdventureWorks";
        connectionInfo.UserID = "V2Application";
        connectionInfo.Password = "wrox";
    
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
        {
            TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
            tableLogOnInfo.ConnectionInfo = connectionInfo;
            table.ApplyLogOnInfo(tableLogOnInfo);
       }
    }

  6. Change the ServerName to your SQL Server and change the UserID and Password to a valid SQL Login to the AdventureWorks database.

  7. Run the project.

You should see your report appear in the browser as shown in Figure 20.

Figure 20. Figure 20

Let me back up for a second and explain what you just did. The first step was to add the Crystal Report Viewer Control to the Default page. The viewer displays the report as shown in Figure 20. By default, it shows the toolbar that has similar buttons to the ones you saw in the Visual Studio Designer. There are buttons to export the report to disk, print the report, show or hide the group tree, and for page navigation, search, and zoom.

The second step had you add a few lines of code to the Page_Load event. The first line creates an instance of a ReportDocument object. This class is found in the CrystalDecisions.CrystalReports.Engine namespace and allows you to upload a report file and manipulate it through code.

ReportDocument report = new ReportDocument();

The second line loads the Employee.rpt file into the report object.

report.Load(Server.MapPath("Employee.rpt"));

The third line calls a custom method that tells the Crystal Report which database contains the tables that are associated with the report. Remember that when you created the report you added the Contact, Department, Employee, and EmployeeDepartmentHistory tables using the Database Expert. When you want to view the report, you must set the location for each table in the report:

SetTableLocation(report.Database.Tables);

The last line sets the report object as the ReportSource for the viewer. This is how it knows which report to display.

CrystalReportViewer1.ReportSource = report;

Now let's take a look at the SetTableLocation method you added. This loops through all the tables defined in the report and sets the location to your database. The first line in this method creates an instance of the ConnectionInfo object. This class is found in the CrystalDecisions.Shared namespace. The four properties you must set are the ServerName, DatabaseName, UserID, and Password.

ConnectionInfo connectionInfo = new ConnectionInfo();

connectionInfo.ServerName = "VARALLOV01";
connectionInfo.DatabaseName = "AdventureWorks";
connectionInfo.UserID = "V2Application";
connectionInfo.Password = "wrox";

The next section loops around all the tables in the CrystalReports tables collection and sets each table's ConnectionInfo property. You then must call the ApplyLogOnInfo method to have the changes take effect.

foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
    TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
    tableLogOnInfo.ConnectionInfo = connectionInfo;
    table.ApplyLogOnInfo(tableLogOnInfo);
}

This is the bare minimum you need to do to display the report. However, there are plenty of options you have to customize the Viewer control and also the Report object. Some of the options you may need to manipulate on the Viewer are:

  • DisplayGroupTree — Shows or hides the group tree on the left side of the report.

  • DisplayToolbar — Shows or hides the toolbar at the top of the report.

  • HasCrystalLogo — Shows or hides the Business Object logo in the toolbar.

  • HasExportButton — Shows or hides the Export button in the toolbar.

  • HasGoToPageButton — Shows or hides the GoTo textbox in the toolbar. The GoTo textbox allows a user to jump to a specific page.

  • HasPageNavigationButtons — Shows or hides the page navigation buttons in the toolbar.

  • HasPrintButton — Shows or hides the Print button in the toolbar.

  • HasRefreshButton — Shows or hides the Refresh button in the toolbar.

  • HasSearchButton — Shows or hides the Search button in the toolbar.

  • HasToggleGroupTreeButton — Shows or hides the group tree. If you aren't grouping your report, you should hide this button.

  • HasZoomFactorList — Shows or hides the dropdown list that allows a user to adjust the zoom for the report.

The Viewer presents a nice view of the report, and as you can see, it is trivial to display a report to a user. However, it does have some drawbacks. Take a look at the e-mail field in the Viewer. Any e-mail address that is longer than the width of the field overlaps the NationalIDNumber field. When you view this report in the Visual Studio Designer, the field gets cut off, so be careful when testing the report because it doesn't always render the same in every browser. Second, when you page through the report, the web page is actually posting back to the server, executing the SQL statement against the database, and binding to the report again. For small reports this may be OK, but for longer-running queries, this can be expensive and appear less professional to the user.

One way to get around both of these limitations is to display the report as a PDF, Word, or Excel document rather than use the Report Viewer control. The following steps will show you how to display the report as a PDF file to the user:

  1. Right-click on the web site and select "Add New Item."

  2. Choose "Web Form" for the type and keep the name as Default2.aspx. Click Add.

  3. Open the code-behind page for Default2.aspx and add the following using directives:

    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;

  4. Copy the SetTableLocation method from Default.aspx.cs to the Default2.aspx.cs.

  5. Add the following code to the Page_Load event:

    protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument report = new ReportDocument();
        report.Load(Server.MapPath("Employee.rpt"));
    
        SetTableLocation(report.Database.Tables);
    
        report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response,
                                    false, "");
    }

  6. Set Default2.aspx as the startup page for the web site, and run the project.

The report should now appear in Adobe Reader, as shown in Figure 21. Notice that the e-mail address appears cut off, just as it does in the Visual Studio Designer. Also, you can page through the report without posting back to the server since the entire report is hosted in Adobe Reader.

Figure 21. Figure 21

If you are wondering how to format the email field so it doesn't get cut off, don't worry, it's easy. Open the report back up in Design mode in Visual Studio. Right-click on the email field and select "Format Object." Check off the box called Can Grow. This tells Crystal Reports to wrap the text to the next line if the field isn't wide enough. You can limit the number of lines the field would wrap to by changing the "Maximum Number of Lines" or leave it at 0 to allow unlimited growth. Click on the OK button and run the project again. The email field should now wrap for any address longer than the width of the field in the report.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset