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

Null Reference Exception when exporting to Excel programmatically

0 Answers 112 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 30 May 2013, 04:47 PM
Hi,

I'm using Telerik Reporting Q1 2013 and I have a report which contains a subreport which I have been able to export to HTML, PDF, CSV, etc., etc., programmatically, but it throws an exception when attempting to export to Excel (XLS/XLSX).  I can export a different report that I have (which contains no subreport) to all formats (including Excel) without any exceptions.  As far as I can tell, the only real difference between the two is the subreport.

I get a Null Reference Exception when calling the RenderReport method of the ReportProcessor class.  Here's the stack trace:

{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":" at Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.RenderingItemContainer.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.Table.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.RenderingItemContainer.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.Section.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.Group.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.RenderingItemContainer.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.Report.RenderInternal(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.RenderingItem.Render(IWorksheet worksheet, TableLayoutInfo layoutInfo)\r\n at Telerik.Reporting.ExcelRendering.Report.Render(IWorkbook workbook)\r\n at Telerik.Reporting.ExcelRendering.Excel97.ExcelReport.Telerik.Reporting.Processing.IRenderingExtension.Render(Report report, Hashtable renderingContext, Hashtable deviceInfo, CreateStream createStreamCallback, EvaluateHeaderFooterExpressions evalHeaderFooterCallback)\r\n at Telerik.Reporting.Processing.ReportProcessor.RenderCore(ExtensionInfo extensionInfo, IList`1 reports, Hashtable deviceInfo, IRenderingContext renderingContext, CreateStream createStreamCallback)\r\n at Telerik.Reporting.Processing.ReportProcessor.ProcessAndRender(String format, ReportSource reportSource, Hashtable deviceInfo, IRenderingContext renderingContext, CreateStream createStreamCallback)\r\n at Telerik.Reporting.Processing.ReportProcessor.ProcessAndRenderStateless(String format, ReportSource reportSource, Hashtable deviceInfo, IRenderingContext renderingContext, CreateStream createStreamCallback)\r\n at Telerik.Reporting.Processing.ReportProcessor.RenderReport(String format, ReportSource reportSource, Hashtable deviceInfo)\r\n at CPSReporting.TelerikCPSReportProcessor.RenderExcelReport(CPSReportDefinition reportDefinition) in c:\\Code\\CPS_SVN_REPO\\CPSReporting\\trunk\\TelerikCPSReportProcessor.cs:line 78\r\n at CreditPoint.Web.Controllers.WebAPI.ReportController.PrepResponse(HttpRequest request) in c:\\Code\\CPS_SVN_REPO\\Web\\branches\\RB-4.6\\Controllers\\WebAPI\\ReportController.cs:line 69\r\n at CreditPoint.Web.Controllers.WebAPI.ReportController.Post() in c:\\Code\\CPS_SVN_REPO\\Web\\branches\\RB-4.6\\Controllers\\WebAPI\\ReportController.cs:line 27\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.b__c(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.b__4()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}

I believe the stack trace is the same for both XLS and XLSX device formats.  As I said, this is only happening with one of my reports (which happens to contain a subreport).  All other reports work just fine when exporting to Excel.

Here's the code:

InstanceReportSource instanceReportSource = new InstanceReportSource();
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.IgnoreWhitespace = true;
ReportXmlSerializer serializer = new ReportXmlSerializer();
XmlReader primaryReportXmlReader = XmlReader.Create(reportDefinition.PrimaryReport.TemplateLocation, xmlReaderSettings);
Telerik.Reporting.Report primaryReport = (Telerik.Reporting.Report)serializer.Deserialize(primaryReportXmlReader);
primaryReportXmlReader.Close();
this.SetConnectionStrings(ref primaryReport, reportDefinition.ConnectionString);
instanceReportSource.ReportDocument = primaryReport;
if (reportDefinition.PrimaryReport.ReportParameters != null)
{
    foreach (KeyValuePair<string, object> item in reportDefinition.PrimaryReport.ReportParameters)
        instanceReportSource.Parameters.Add(this.CreateParameter(item.Key, item.Value));
}
foreach (CPSReport cpsSubReport in reportDefinition.SubReports)
{
    XmlReader xmlReader = XmlReader.Create(cpsSubReport.TemplateLocation, xmlReaderSettings);
    Telerik.Reporting.Report subReportInstance = (Telerik.Reporting.Report)serializer.Deserialize(xmlReader);
    xmlReader.Close();               
    this.SetConnectionStrings(ref subReportInstance, reportDefinition.ConnectionString);
    Telerik.Reporting.SubReport subReport = primaryReport.Items.Find(cpsSubReport.ReportName, true).FirstOrDefault() as Telerik.Reporting.SubReport;
    ReportSource subReportSource_Type = subReport.ReportSource;
    InstanceReportSource subReportSource_Instance = new InstanceReportSource();
    subReportSource_Instance.ReportDocument = subReportInstance;
    foreach(Telerik.Reporting.Parameter param in subReportSource_Type.Parameters)
        subReportSource_Instance.Parameters.Add(param);
    subReport.ReportSource = subReportSource_Instance;
}
Hashtable settings = new Hashtable() {
    { "UseNativePageHeader", false },
    { "UseNativePageFooter", true },
    { "ShowGridLines", false }
};
//Exception thrown here:
RenderingResult result = _reportProcessor.RenderReport("XLS", instanceReportSource, settings);
return result.DocumentBytes;

Thank you in advance,
An otherwise extremely happy Telerik user  :)

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Adam
Top achievements
Rank 1
Share this question
or