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

Dynamically assigning report classes in runtime viewer

7 Answers 185 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Tomica
Top achievements
Rank 2
Tomica asked on 18 May 2011, 09:30 PM
I have built a generalized report viewer page, with up to 50 potential reports to select from, depending on the context.

I realize that each report is a "class" and therefore you cannot just treat it like you would assign a value to a parameter.

But I'm wondering if there is a way to replace this select and the hard-coding of the report classes, into something more dynamic.

I've tried to move the SQL setup outside the case logic, reducing the repeated lines by half, but the report object loses scope outside the case statement, and the compiler balks.

I've seen another thread that does something similar with C, but I don't know enough to translate it into VB. (VS2010)

Select Case Me.Report_Selection_Combo.SelectedValue
    Case "publication_summary_report"
        Dim report1 As New Publication_Summary_Report
        ReportViewer1.Report = report1
        Dim sqlDataSource1 As Telerik.Reporting.SqlDataSource = DirectCast(report1.DataSource, Telerik.Reporting.SqlDataSource)
        sqlDataSource1.SelectCommand = StrConv(HttpContext.Current.Session("sql_select"), VbStrConv.Lowercase)
          
    Case "publication_standard_report"
        Dim report1 As New Publication_Standard_Report
        ReportViewer1.Report = report1
        Dim sqlDataSource1 As Telerik.Reporting.SqlDataSource = DirectCast(report1.DataSource, Telerik.Reporting.SqlDataSource)
        sqlDataSource1.SelectCommand = StrConv(HttpContext.Current.Session("sql_select"), VbStrConv.Lowercase)
    Case "publication_catalog_report"
        Dim report1 As New Publication_Catalog_Report
        ReportViewer1.Report = report1


line 2

7 Answers, 1 is accepted

Sort by
0
Accepted
Chavdar
Telerik team
answered on 24 May 2011, 05:18 PM
Hello Tomica,

Usually the DropDownList(ComboBox) items provide two members: Text and Value. The Text property specifies what the user should see as text and the Value property contains additional information associated with it. In your case, you can use the Value of the list item to hold the assembly qualified name of the report. Having the assembly qualified name of the report gives us the opportunity to instantiate different reports with a couple of lines. For example:

' Example how to create a report from its assembly qualified name
Dim reportType As Type = Type.GetType(Me.Report_Selection_Combo.SelectedValue)
Dim report As IReportDocument = CType(Activator.CreateInstance(reportType), IReportDocument)
 
Me.reportViewer1.Report = report

To get dynamically the reports you can use a similar approach to the one implemented in the ReportManager.GetReports() method which you can find in the ReportLibrary project from the examples. Additionally you have to iterate through the collected report infos and populate the list items with the respective information, e.g.: Text = ReportInfo.Name (or Description), Value = ReportInfo.ReportType.AssemblyQualifiedName.

Hope this helps.

Greetings,
Chavdar
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Muhammad
Top achievements
Rank 1
answered on 29 Aug 2012, 10:19 AM
Anybody have c# equivalent for this code? Thanks for sharing.

-Awais
0
Chris Gillies
Top achievements
Rank 1
answered on 29 Aug 2012, 11:00 AM
http://converter.telerik.com/

Cheers
0
Muhammad
Top achievements
Rank 1
answered on 29 Aug 2012, 11:51 AM
well i figured it out, here is the code for those who are working with C#


        Type reportType = Type.GetType(this.ddlReportList.SelectedValue);
        IReportDocument report = (IReportDocument)Activator.CreateInstance(reportType);
        this.ReportViewer1.Report = report;
0
Muhammad
Top achievements
Rank 1
answered on 29 Aug 2012, 11:54 AM
Thanks Chris, the converter thingy works! :-)
0
Tomica
Top achievements
Rank 2
answered on 29 Aug 2012, 05:24 PM
Chris,

Thanks for the Converter page.

I find myself attempting to translate C demos and sample code in books to VB the old-fashioned way.

And despite being in the programming business 38 years, I just don't "get" the C language for my projects.

Now I can farm-out the heavy-lifting to the converter service.
0
Rielly
Top achievements
Rank 1
answered on 13 Aug 2013, 08:22 PM
I have this exact same scenario.  The only thing is that ReportViewer.Report is now obsolete.  Is there a scenario that uses ReportViewer.ReportSource?
Tags
General Discussions
Asked by
Tomica
Top achievements
Rank 2
Answers by
Chavdar
Telerik team
Muhammad
Top achievements
Rank 1
Chris Gillies
Top achievements
Rank 1
Tomica
Top achievements
Rank 2
Rielly
Top achievements
Rank 1
Share this question
or