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

Templates and Reports

1 Answer 108 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 23 Jul 2012, 07:36 PM
Hello,

I'm trying to find a dynamic way to Generate reports. So say I have:

Report 1
Report 2
Report 3

Typically you would create a report like this in my web app. 
namespace ReportingSuite.Reports
{
    public partial class AgentPPLReport : OLAPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ruid = "18e1bcca-b432-47a2-90ed-f982ac6225bb";
            if (ValidateReport(Ruid))
            {
                IReportDocument r = null;
                r = new AgentPPL();
                ReportViewer1.Report = r;
                ReportViewer1.RefreshReport();
            }
        }
    }
}


But I hate re-writing similar pieces of code.  It's bad practice repeating code.
And foreach report there everything is going to be the same except the the line where you initialize the report.

What I would like to do instead of writing a new page for each separate report is have one generic page that you send a report type to.  I want to say there is some way around this using templates in C# but answer is eluding me.

1 Answer, 1 is accepted

Sort by
0
Ed Lance
Top achievements
Rank 1
answered on 24 Jul 2012, 08:11 PM
Matt,
Not sure if this is what you want, but I use one page in the site, rptviewer.aspx, and pass a querystring parameter for the report that it is to display. 
The REPORTTYPE constant should use the name of the assembly the report classes are in.  The GetReportPath function takes the parameter and translates that to the actual class name of the report.  I use a database table to store that mapping, but you could do whatever you want.
#Region "Module Variables"
 
    Private Const REPORTTYPE As String = "PMP2Reports.{0}, PMP2Reports"
#End Region
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
 
        If Not IsPostBack Then
            Dim report1 As Telerik.Reporting.Report
            Dim objSession As Object
            Dim strReport As String
 
            'Use the querystring to look up the report's name
            If Len(Request.QueryString("r")) > 0 Then
                strReport = GetReportPath(Request.QueryString("r"))
            End If
 
            'If no report name
            If strReport = "" Then
                Response.Redirect("~/Default.aspx")
            End If
 
            'Create an instance of the report class
            Dim T As Type = Type.GetType(String.Format(REPORTTYPE, strReport), True)
 
            report1 = Activator.CreateInstance(T)
 
            'Get the case index value and set the report parameter
            objSession = Session("subIdx")
 
            If Not objSession Is Nothing Then
                If report1.ReportParameters.Contains("subIdx") Then
                    report1.ReportParameters("subIdx").Value = objSession
                End If
            End If
 
            ReportViewer1.Report = report1
 
        End If
 
    End Sub
Tags
General Discussions
Asked by
Matt
Top achievements
Rank 1
Answers by
Ed Lance
Top achievements
Rank 1
Share this question
or