This is a migrated thread and some comments may be shown as answers.

Parameterizing the opening of a report

1 Answer 49 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Steven Black
Top achievements
Rank 1
Steven Black asked on 20 Oct 2011, 01:57 PM
Hello,

I have a generic page that has a ReportViewer control on it.  The Page_Load event collects some querystring parameters and uses them to open the appropriate report.  For example, here is some sample code:

Select Case intReportID
      Case 1
           rptViewer.Report = New MyReportingProject.Report1(param1, param2, param3)
      Case 2
           rptViewer.Report = New MyReportingProject.Report2(param1, param2, param3)
      Case 3
           rptViewer.Report = New MyReportingProject.Report3(param1, param2, param3)
End Select

My real application will eventually have well over 100 reports.  I'm looking for a way to parameterize this so that I don't need the Select Case statement at all.  For example, in my database I have a table that tracks the IDs and associated reports.  Is there a way for me to do something like this:

rptViewer.Report = New Report([strReportName])(parameters need to be passed to the New method of the report as well)

The parameters are always identical as well.

Thanks.

Steve

1 Answer, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 25 Oct 2011, 10:31 AM
Hello Steven Black,

You can find the report constructors and instantiate the Report with reflection from the assembly qualified name as shown in the following code snippet:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
 
        Dim reportType As Type = Type.[GetType](Request.QueryString("report"))
        Dim ctor As ConstructorInfo = reportType.GetConstructor(New Type() {GetType(String)})
        Dim report As Report = DirectCast(ctor.Invoke(New Object() {"my custom value"}), Report)
 
        'set the report
        Me.ReportViewer1.Report = report
    End If
End Sub

Check out the attached sample that illustrates the suggested approach.

Regards,
Peter
the Telerik team

Q2’11 SP1 of Telerik Reporting is available for download (see what's new). Get it today.

Tags
General Discussions
Asked by
Steven Black
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or