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

How to load report using report viewer programmatically

16 Answers 2631 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 23 Apr 2009, 06:59 PM
I have an aspx page with a list of reports.  User clicks one of the report, this opens up a new window to load the report but I have only one report viewer attached on the form.  I want to use code-behind to load the selected report (being passed via URL parameter).  How do I in code behind, load the report using the report viewer control.  There is something about IReportDocument but I'm not sure how to use that to load the report in code behind.

Please show me a sample code, I'm using the latest Q12009 Telerik Reporting.

Thanks,
Kevin

16 Answers, 1 is accepted

Sort by
0
Sekar
Top achievements
Rank 1
answered on 24 Apr 2009, 12:01 AM
Hi Kevin
   You can use the single report viewer to load any number of reports at runtime. Below is a example snippet to load report class to the report viewer at runtime. 
IReportDocument runtimeReport; 
switch(rptType) 
case ReportType.Summary: runtimeReport = new SummaryReport(); break
case ReportType.Invoice: runtimeReport = new InvoiceReport(); break
default: runtimeReport = new CustomerReport(); break
this.rptViewer1.Report = runtimeReport; 
 


Hope this one helps. Let me know if you need more info

Regards
Sekar Ramamurthy
0
Kevin
Top achievements
Rank 1
answered on 24 Apr 2009, 05:37 AM
Thanks Sekar for sample snippet.  This works greatly, much appreciated!!!
0
Brian
Top achievements
Rank 1
answered on 20 May 2009, 02:30 PM
I tried loading a report like posted above but I get a compiler error.  I tried casting it as IReportDocument and Report but it gave a cast exception.  What am I missing?

Compiler Error Message: CS0266: Cannot implicitly convert type 'Glossary' to 'Telerik.Reporting.IReportDocument'. An explicit conversion exists (are you missing a cast?)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Configuration; 
using AWS_Reports; 
using Telerik.Reporting; 
 
 
public partial class GlossaryReport : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
             
            IReportDocument runtimeReport; 
            runtimeReport = new Glossary(); 
            ReportViewer1.Report = runtimeReport; 
        } 
    } 




0
Steve
Telerik team
answered on 20 May 2009, 03:13 PM
Hi Brian,

Your code by itself works, as can be seen from the sample project I've attached. Why don't you instantiate your report like so:

Glossary runtimeReport = new Glossary(); 
this.ReportViewer1.Report = runtimeReport;

This way we do not specify whether it is a report or report book and it should be accepted by the viewer.

Best wishes,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Aditya Choudhari
Top achievements
Rank 1
answered on 15 Jun 2010, 09:21 AM
Hi,

I used th provided snippet and the reports loaded successfully as per radio button selection.....

However, when I try to export the report to Excel or CSV it fails and shows this error

Parameter is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Parameter is not valid.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: Parameter is not valid.]
   System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) +1046933
   System.Drawing.Bitmap..ctor(Int32 width, Int32 height) +14
   Telerik.Reporting.ExcelRendering.PictureBox.GetAdjustedImage(Image originalImage, RectangleU srcRectangle, RectangleU dstRectangle) +755
   Telerik.Reporting.ExcelRendering.PictureBox.ProcessImage(Image originalImage, RectangleU& excelRectangle, Image& excelImage) +10209
   Telerik.Reporting.ExcelRendering.PictureBox.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo) +104
   Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo) +138
   Telerik.Reporting.ExcelRendering.RenderingItemContainer.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo) +100
   Telerik.Reporting.ExcelRendering.Section.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo) +15
   Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo) +138
   Telerik.Reporting.ExcelRendering.RenderingItemContainer.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo) +100
   Telerik.Reporting.ExcelRendering.Report.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo) +450
   Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo) +138
   Telerik.Reporting.ExcelRendering.Report.Render(IWorkbook workbook) +232
   Telerik.Reporting.ExcelRendering.Excel97.ExcelReport.Telerik.Reporting.Processing.IRenderingExtension.Render(Report report, Hashtable renderingContext, Hashtable deviceInfo, CreateStream createStreamCallback, EvaluateHeaderFooterExpressions evalHeaderFooterCallback) +81
   Telerik.Reporting.Processing.ReportProcessor.Render(IList`1 reports, ExtensionInfo extensionInfo, Hashtable renderingContext, Hashtable deviceInfo, CreateStream createStreamCallback) +724
   Telerik.Reporting.Processing.ReportProcessor.RenderReport(String format, IReportDocument reportDocument, Hashtable deviceInfo, Hashtable renderingContext, CreateStream createStreamCallback) +377
   Telerik.ReportViewer.WebForms.ServerReport.Render(HttpResponse response, String format, Int32 pageIndex) +460
   Telerik.ReportViewer.WebForms.ReportExportOperation.PerformOperation(NameValueCollection urlQuery, HttpContext context) +60
   Telerik.ReportViewer.WebForms.HttpHandler.ProcessRequest(HttpContext context) +164
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


the report is exported to PDF as expected but fails for other formats....Also when same report is specifed statically through source, everything works....there is problem with dynamic loading..Any idea why this is happening???

Thanks
Aditya




0
Steve
Telerik team
answered on 16 Jun 2010, 06:12 PM
Hello Aditya,

It seems there is some problem with an image you have loaded in a pictureBox item. You can pinpoint the problematic one (if more than one) and try placing it in a single report to see if the problem would persist. If this is the case, please provide us with a sample report where the issue can be observed and we would advise you accordingly.

Sincerely yours,
Steve
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
Patrick
Top achievements
Rank 1
answered on 02 Jul 2010, 08:34 PM
Nevermind... I found an example in the CSharp.WinDemo project

Sekar,

Would you please expand on the example you've given? I'm assuming that ReportType is an enum?

Thanks!


IReportDocument runtimeReport;    
switch(rptType)    
{    
  case ReportType.Summary: runtimeReport = new SummaryReport(); break;    
  case ReportType.Invoice: runtimeReport = new InvoiceReport(); break;    
  default: runtimeReport = new CustomerReport(); break;    
}    
this.rptViewer1.Report = runtimeReport;    
 
0
Abirami
Top achievements
Rank 1
answered on 28 Feb 2011, 05:11 AM
Steve,

I am also facing the same problem while exporting the report to excel.

Is this error occurs due to the dimension of the image we are using in the report?

Is there any size restriction to be set for the report?

Awaiting for your response.

Thanks,
Abirami
0
Steve
Telerik team
answered on 28 Feb 2011, 05:38 PM
Hello Abirami,

Are you referring to the "Parameter is not valid." error? If so, please open a support ticket and attach a sample report that exhibits the problem. Once we review it, we would be able to provide more information.

Greetings,
Steve
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Prashant Khandalkar
Top achievements
Rank 2
answered on 13 Mar 2012, 07:29 AM
I want to bind DataTable with Telerik Report at run time so wrote following code. But, nothing is displayed in the Telerik Report.It shows blank.
protected void Dataview(XmlDocument resultsXML)
    {      
        XmlNodeList entries = resultsXML.GetElementsByTagName("entry");     
        DataTable table = new DataTable();
        table.Columns.Add("Page Path", typeof(string));
        table.Columns.Add("Pageviews", typeof(string));
        table.Columns.Add("Unique page views", typeof(string));
        table.Columns.Add("Pageviews Per Visit", typeof(string));
        for (int i = 0; i < entries.Count; i++)
        {
            string dimension = entries[i].ChildNodes[4].Attributes["value"].Value;
            string metric = entries[i].ChildNodes[5].Attributes["value"].Value;
            string metric1 = entries[i].ChildNodes[6].Attributes["value"].Value;
            string metric2 = entries[i].ChildNodes[7].Attributes["value"].Value;
            table.Rows.Add(dimension, metric, metric1, metric2);         
        }          
            radReportViewer.Visible = true;
            radReportViewer.Report = (Telerik.Reporting.IReportDocument)table;
            radReportViewer.DataBind();
  }

please solve my problem.
0
Steve
Telerik team
answered on 13 Mar 2012, 09:00 AM
Hi,

A possible reason for not having any data in your report is if you have not provided any database columns, the report needs valid Expressions in order to display data. In other words, the report would not auto generate columns/rows based on your data, you have to manually set valid expressions (e.g. = Fields.MyFirstColumn) in the TextBox items. If your bind to lots of database columns, this could be a tedious process, so that is why we have provided you with a Report Wizard that would help you in designing your table and we highly recommend using it. As a new user, we also recommend reviewing the Designing Reports help section from the documentation, so you can get familiar with the product and the way it functions.
Specifically for binding to a DataTable, we recommend using the ObjectDataSource Component. For more info see How to: Bind to a DataTable.

Regards,
Steve
the Telerik team
NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
0
Prashant Khandalkar
Top achievements
Rank 2
answered on 16 Mar 2012, 06:12 AM
Hi Steve,

   Thx for your help. I got success to generate report somehow. Now I want to make a chart in telerik report. I did it by passing report's datasource to chart's datasource but it generates unreadable chart. So many data displayed on the chart. Actually I want to generate pagewise chart as generated in report. e.g. only 10 records shown on chart as shown in one report page. So, is there a way by which I can get report's textbox value at runtime and pass it to the chart as a data source. Please help i m having an urgent need for that.
0
Steve
Telerik team
answered on 16 Mar 2012, 09:19 AM
Hi Prashant,

You can either apply a filter to the chart item especially if it is in the detail section of the report, so that the data that it shows is shaped according to the current data record or if you just want to display a certain amount of data, you can limit the displayed values in the chart from PlotArea -> XAxis -> MaxItemsCount - you can set it to the number of items you want to be displayed.

Regards,
Steve
the Telerik team
NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
0
Muzammil
Top achievements
Rank 1
answered on 24 Sep 2012, 05:30 AM
Hi Prashant ,

I want to know your solution regarding loading report from datable at run time. Thanks.

Regards,
Muzammil
0
Randhir
Top achievements
Rank 1
answered on 05 Sep 2013, 12:48 PM
I have used below code..

ImportHistoryRpt report = new ImportHistoryRpt();//report instance but didn't put any control like table,textbox etc
               // Telerik.Reporting.Report report = new Telerik.Reporting.Report();
                ReportViewer rvw = new ReportViewer();

                rvw.Width = Unit.Percentage(100);

                rvw.Height = Unit.Percentage(100);

                phReports.Controls.Add(rvw);

                report.DataSource = dt; // some custom business object of mine...  

                rvw.Report = report;  


                //// Creating a new report
                //Telerik.Reporting.Report report = new Telerik.Reporting.Report();

                ////// Assigning the ObjectDataSource component to the DataSource property of the report.
                //report.DataSource = dt;
                //rptImportHisViewer.Report = report;

                //// Use the InstanceReportSource to pass the report to the viewer for displaying
                Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
                reportSource.ReportDocument = report;

                // Assigning the report to the report viewer.
                rvw.ReportSource = reportSource;

Its displayed 1 of 31 under the top toolbar..but report is empty.Please suggest me what is missing in the above code..
0
Stef
Telerik team
answered on 05 Sep 2013, 04:39 PM
Hi Randhir,

There is no auto-generate feature in Telerik Reporting, thus your report definition should contain items using the fields of the set data. More details about creating a report definition can be found here.

If you are using events to provide data to the report or data items in it, assure the corresponding events are fired. You can check the following help articles elaborating on using events:

In case none of the above relates to your issue, please open a support ticket and send us your report definition and some sample data to run it.

Regards,
Stef
Telerik

Have you tried the new visualization options in Telerik Reporting Q2 2013? You can get them from your account.

Tags
General Discussions
Asked by
Kevin
Top achievements
Rank 1
Answers by
Sekar
Top achievements
Rank 1
Kevin
Top achievements
Rank 1
Brian
Top achievements
Rank 1
Steve
Telerik team
Aditya Choudhari
Top achievements
Rank 1
Patrick
Top achievements
Rank 1
Abirami
Top achievements
Rank 1
Prashant Khandalkar
Top achievements
Rank 2
Muzammil
Top achievements
Rank 1
Randhir
Top achievements
Rank 1
Stef
Telerik team
Share this question
or