Binding to a Strongly Typed Cached Report

Another option for strongly typed reports is the ability to use ASP .NET caching with your report. When you added your report to your application, you may have noticed that there was a checkbox for Generate cached strongly typed report. Report caching is based on the underlying ASP .NET caching model and provides an easy way to improve your application's performance.

When a report is cached, the subsequent Web Form that is used to view the report will load faster when accessed by different users. To add a report to your Web Form as a cached report, select this option as you add a strongly typed report to your application from the dialog shown in Figure 5-13.

You will see that your report file will be inserted as cached_sales_graph1 and an additional object will be inserted into the Web Form's source file.

Figure 5-13

You could then bind to this particular cached report just as you would to any other strongly typed report, but with a different name:

CrystalReportViewer1.ReportSource = cached_sales_graph1

When multiple users visit the same report, they will actually be looking at a cached copy and not hitting the database in real time. (To ensure this is true, the viewer by default does not have a refresh button showing.)

So regardless of which method you choose to bind your report to the viewer, the result is the same. For ease of use and functionality provided, the easiest method is going to be to stick with strongly typed reports, because in the long run, the structure and coding standards will mean you can create reporting applications quickly, in a consistent manner.

After binding, you can run your application and when the form is loaded, the Crystal Report Viewer will run the report you have set in the ReportSource property and display a preview of it. Before we can move on to customizing the viewer, however, we need to look at working with secured databases.

Before we finish up with viewer basics and binding, keep in mind that reports that were created from a secure data source may require a user name and password.

To add ch5_worldsales_northwind.rpt to your project, select Project ^ Add New Item and browse for the report where you unzipped the downloaded sample files. Add it to the project and click the Open button.

Now drag and drop a CrystalReportViewer onto your form. For the sake of attractiveness, set the Dock property of the form to Fill.

You will also need to add this report as a component to your form. Switch to the Layout view of your form and look in the toolbox under Components. In this section, you should see a component labeled ReportDocument. Drag this component onto your form.

From the drop-down list, select the ch5_worldsales_Northwind report and click OK. We are now ready to get on with setting the database credentials for this report.

This report has been written from a single table, Customer, for which we are setting the Connectionlnfo. The name of our server is localhost, off of the Northwind database, with sa as the user ID, and no password.

Add this code to the Form1_Load method:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myReport = New ch5_worldsales_northwind()

Dim myTableLogonlnfos = New CrystalDecisions.Shared.TableLogOnInfos() Dim myTableLogonlnfo = New CrystalDecisions.Shared.TableLogOnInfo() Dim myConnectionlnfo = New CrystalDecisions.Shared.ConnectionInfo()

With myConnectionlnfo

.ServerName = "localhost" .DatabaseName = "Northwind" .UserlD = "sa" .Password = "" End With myTableLogonlnfo.Connectionlnfo = myConnectionlnfo myTableLogonlnfo.TableName = "customers" myTableLogonlnfos.Add(myTableLogonlnfo) CrystalReportViewerl.LogOnlnfo = myTableLogonlnfos CrystalReportViewerl.ReportSource = myReport

End Sub

Make sure that when you are finished setting the Connectionlnfo for the table, you specify the table name you are working with prior to using the Add method; otherwise you will receive an error message.

Compile and run the example. The report should appear, looking identical to the previous examples.

This is a very simple example, as our report has only one table to worry about. If you have a report that features multiple tables or if you don't know the names of the tables, you could also set up a loop to go through each report.database.table in report.database.tables and set the Connectionlnfo properties for each.

In order to get all of the tables in your report and loop through them, you will need to use the Report Engine, which is covered in Chapter 9, "Working with the Crystal Reports Engine."

Was this article helpful?

0 0

Post a comment