[Download Sample App]
This series of articles will walk through a sample application that loads a Crystal Report and displays the parameters the report needs programmatically. When the user enters the parameter information, the application then calls and displays the report.
I intend for this sample application to be extendable. Think of building your own database of reports and allowing users to select the reports to display, or some other permutation I haven't thought of.
I’ve separated this series of articles in three installments. First, we’ll go through the initial display of the parameters on the web page. Next, we’ll go through the class that actually determines the parameters and how to display them on the page. And finally, we’ll log onto the report and display the report as a PDF.
These articles assume familiarity with ASP.NET and Crystal Reports .NET. For more information on Crystal Reports with .NET, be sure to visit the Crystal Alliance site at http://www.aspalliance.com/crystal. The downloadable sample application is written in VB.NET.
Getting the Parameters from the Report
The following information is based on the sample application I’ve included with this series of articles. The sample solution application includes a web project and a class project written in VB.NET. Let me briefly tell you about the class file. I’ll cover parts of it in depth in the second installment of this article.
The class PCRMCreateCRObjects will create a collection that contains the objects to place on the page. The main parts of this class are the controlsCollection collection and the GetBuilder method. controlsCollection contains the web form controls that are added to the user input page.
I start out with a page, _displayCR.aspx that contains the controls for user input. This page on initial examination only contains a Panel control. The code-behind for this page will add controls for parameter input based on the report that is used. The page in the sample application is a bit bland, and begs for you to add some style to it; however, it does give you a great idea of the nuts and bolts necessary to programmatically set up your parameter input controls.
In page_load, the CreatePage method is called. CreatePage instantiates a new PCRMCreateCRObjects class to create the parameter controls. A report name can be passed (I’ve used a querystring that includes a file identifier), but for the purposes of this sample, the report name is hard coded.
Depending on the type of parameters, CreatePage will put the correct server controls in the panel. For instance, if a parameter is a boolean type, a CheckBox server control will be placed in the panel. The GetBuilder method determines the correct server control.
The object oCrystalEntry is an instance of a custom class PCRMCreateCRObjects. This custom class does the work of finding the parameters that this Crystal Report may have. The method we use is called GetBuilder. This method is described later in the article.
Public Sub CreatePage()
Dim oCrystalEntry As New PCRMCreateCRObjects()
Dim oEntry As Object
' Get the filename and location from the PCRMFile object
The GetBuilder method builds all the parameter information into the instantiated object. This includes the header information, which is added to the Panel object on the page first. Once that is added, other style HTML can be added to the panel. In the sample app, a break is added.
Now we begin iterating through the parameters that the report has, using the collection controlsCollection.
For Each oEntry In oCrystalEntry.controlsCollection
' This assumes that labels for the control are
' placed after the control
If UCase(oEntry.GetType.FullName) <> UCase(labelName) Then
Dim _literalBr As New System.Web.UI.LiteralControl()
_literalBr.Text = "<br>"
oEntry.id = oEntry.ParamID & "1"
_literalBr.ID = "literal" & Trim(oEntry.labelid)
_literalBr = Nothing
This part of the method adds the control that was determined by the GetBuilder method for this parameter into the page. It also adds a label and an ID. I use a naming scheme for the ID consistently by adding the paramid + 1 to the control's ID in the collection. This naming scheme could be improved--I may be improving this later--but it works for now.
_literalBr is a LiteralControl to add the HTML tag <br> to the panel. Once all objects are iterated through, a submit button and a hidden form element are added to the panel. The hidden form element is not necessary in the sample app, but when you are passing file names via a querystring or other method, it allows you to make this application flexible for many reports.
In Part II, we'll go over the class that builds the parameters.
More Crystal Resources