Really Basic Report Question

0 Answers 82 Views
Report Viewer - HTML5 WebForms
Bernie
Top achievements
Rank 1
Bernie asked on 02 May 2022, 05:04 PM

I working on learning Telerik reporting to replace our current ReportViewer implementations. Right now I'm trying to get a simple test case working. We current make a typed datatable as our report source and have an assortment of parameters to display on the report.  I'm having trouble finding the basic syntax to do this. 

Let's assume I have a the following pieces:

  • Report  - rptTestReport.cs
  • ReportViewer - ReportViewer1
  • Parameter on rptTestReport.cs called prmTestParameter
  • DataTable - dtMyData

How do I assemble all this together, connection the report to the reportviewer, the parameters to the report, and the datatable to the report?

This is what I have so far, but attaching the parameter to the report is wrong.

            UriReportSource reportSource = new UriReportSource();
            reportSource.Uri = "rptTest.cs";
            reportSource.Parameters.Add(new Telerik.Reporting.Parameter("prmTestParameter", "Hello World!"));
            ReportViewer1.ReportSource(reportSource);

I've found documents showing how to hard code the report to the reportviewer, but I need to do it in code.

Can anyone touch up my code with the right syntax to push me in the right direction?

Neli
Telerik team
commented on 03 May 2022, 02:39 PM

Hi Bernie,

You can easily add the report viewer to your WebForms application by using the Telerik Web Forms Report Viewer Form Item Template

When it comes to using a DataTable for the report's datasource, there are two approaches that I can suggest.

1. If you want to set it in design time, you can use ObjectDataSource.

2. If you want to assign it in runtime, then you will need to implement CustomReportSourceResolver as explained in the How to Implement a Custom Report Source Resolver. For example:

- In the ReportsController, you need to use the CustomReportSourceResolver for the ReportSourceResolver property:

var resolver = new CustomReportSourceResolver();

//Setup the ReportServiceConfiguration
configurationInstance = new ReportServiceConfiguration
    {
        HostAppId = "Html5App",
        Storage = new FileStorage(),
        ReportSourceResolver = resolver,
    };

Below is a sample implementation of the CustomReportSourceResolver. It makes an instance of the CS report, attaches a DataSource to it and returns it as an InstanceReportSource:

public class CustomReportSourceResolver : IReportSourceResolver
{
    public ReportSource Resolve(string report, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
    {
            var reportDef = new Report1();
            reportDef.DataSource = getDT();
            InstanceReportSource rs = new InstanceReportSource() { ReportDocument = reportDef };
            return rs;
    }
    private DataTable getDT()
    {
        DataTable dx = new DataTable("Example");
        dx.Columns.Add("id", typeof(int));
        dx.Columns.Add("name", typeof(string));

        dx.Rows.Add(new object[] { 1, "Mary"});
        dx.Rows.Add(new object[] { 2, "Paul"});
        dx.Rows.Add(new object[] { 3, "Jane"});
        dx.Rows.Add(new object[] { 4, "Pete"});
        dx.Rows.Add(new object[] { 5, "Jose"});

        return dx;
    }
}

In Default.aspx.cs, set the report viewer's ReportSource:

reportViewer1.ReportSource = new ReportSource()
{
    IdentifierType = IdentifierType.CustomReportSource,
    Identifier = typeof(Report1).AssemblyQualifiedName,
};

Then, to set the values of the parameters, you can do it in the Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var rs = new ReportSource();
        rs.IdentifierType = IdentifierType.CustomReportSource;
        rs.Identifier = typeof(Report1).AssemblyQualifiedName;
        rs.Parameters.Add("Parameter1", 123);
        reportViewer1.ReportSource = rs;
    }
}

or in the CustomReportSourceResolver"

reportDef.ReportParameters["Parameter2"].Value = "51212";

I attached a sample project for further reference.

I hope this information helps you. Please, let us know if you need further help.

No answers yet. Maybe you can help?

Tags
Report Viewer - HTML5 WebForms
Asked by
Bernie
Top achievements
Rank 1
Share this question
or