Dynamically assigning report classes in runtime viewer

8 posts, 1 answers
  1. Tomica
    Tomica avatar
    147 posts
    Member since:
    Apr 2008

    Posted 18 May 2011 Link to this post

    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
  2. Answer
    Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 24 May 2011 Link to this post

    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
  3. DevCraft banner
  4. Muhammad
    Muhammad avatar
    10 posts
    Member since:
    Aug 2012

    Posted 29 Aug 2012 Link to this post

    Anybody have c# equivalent for this code? Thanks for sharing.

    -Awais
  5. Chris Gillies
    Chris Gillies avatar
    384 posts
    Member since:
    May 2006

    Posted 29 Aug 2012 Link to this post

    http://converter.telerik.com/

    Cheers
  6. Muhammad
    Muhammad avatar
    10 posts
    Member since:
    Aug 2012

    Posted 29 Aug 2012 Link to this post

    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;
  7. Muhammad
    Muhammad avatar
    10 posts
    Member since:
    Aug 2012

    Posted 29 Aug 2012 Link to this post

    Thanks Chris, the converter thingy works! :-)
  8. Tomica
    Tomica avatar
    147 posts
    Member since:
    Apr 2008

    Posted 29 Aug 2012 Link to this post

    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.
  9. Rielly
    Rielly avatar
    1 posts
    Member since:
    Jul 2012

    Posted 13 Aug 2013 Link to this post

    I have this exact same scenario.  The only thing is that ReportViewer.Report is now obsolete.  Is there a scenario that uses ReportViewer.ReportSource?
Back to Top
DevCraft banner