Binding to an Untyped Report

When working with Crystal Reports .NET, you can add individual report files to your project and use and reference them to print reports from your application. Taking this a step further, you could also use these reports as components, which is where we start to look at typing.

As mentioned earlier, when integrating reports into an application, we can either use strongly typed reports or untyped reports. If you have been working with Visual Studio .NET for any length of time, you have probably heard of strongly typed objects. A strongly typed object is predefined with a number of attributes that are specific to that object, giving programmers more structure and a rigorous set of rules to follow, thus making coding easier and the code more consistent.

Within the frame of reference of Crystal Reports .NET, a strongly typed report can be any report that has been added to your project. What determines the type of a report is how it is added and used within your application. When you first add a report to your application, it is considered untyped — the same goes for any external reports that you may reference (such as the report we pointed to earlier, using the file path C:\Crystal.NET2003\Chapter04\ch4_worldsales.rpt).

In the following example, we are going to look at binding to an untyped report. The first step in binding to an untyped report is to add a ReportComponent to the form we are working with.

To add a report 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, which will open the shown in Figure 4-10.

Figure 4-10

This is the we use to set whether our report document component is typed or untyped. If you select Untyped ReportDocument, a report component named ReportDocumentl is created and we can then load a report into this component and bind the component to the viewer.

Again, the code to perform the binding will appear in your form's Form1_Load method and will look something like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ReportDocumentl As New ReportDocument()

ReportDocument1.Load("c:\Crystal.NET2003\Chapter04\ch4_worldsales.rpt") CrystalReportViewerl.ReportSource = ReportDocumentl End Sub

When your application is run and the form previewed, the viewer should show a preview of the report and allow you to print, export, and perform other functions.

If you are experiencing problems binding, check the file name and path of the report you are working with-alternately, if you are binding to a report that exists within your application, check that you have the correct report name.

+1 0

Post a comment