1
votes

I'm trying to build a method in C# to get SSRS Reportviewer to generate a PDF output.

There are lots of information about the subject so it seemed to be an easy task.

The issue appears when my DataSource come from an XML URL.

This is my method:

private void GetPDF(Stream RDL)
{
    Warning[] warnings = null;
    string[] streamIds = null;
    string mimeType = string.Empty;
    string encoding = string.Empty;
    string extension = string.Empty;
    string filetype = string.Empty;

    ReportViewer RPT = new ReportViewer();
    RPT.ProcessingMode = ProcessingMode.Local;
    RPT.LocalReport.LoadReportDefinition(RDL);
    RPT.LocalReport.Refresh();

    byte[] Bytes = RPT.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

    using (FileStream PDF = new FileStream("test.PDF", FileMode.Create))
    {
        PDF.Write(Bytes, 0, Bytes.Length);
    } 
}

The report works just fine from the ReportBuilder desktop app. As you can see, I removed all the dynamic content (Report URL, Parameters, etc) to simplify and detect the problem source.

On desperation I also tried embedding the XML data in the report.

The error is always the same:

Cannot create a data reader for dataset (the name of the first dataset on the report)

The dataset Query looks like this:

<Query>
<ElementPath IgnoreNamespaces='true'>
Object1 {
    Element1,
    Element2
}
</ElementPath>
</Query>

Please Help!!

SOLUTION:

I got this solved and I want to share my solution so you don't invest so much time on the same.

The problem was generated by the Report Viewer Control as it has no "Query" capabilities.

Then, my solution was to build a Dataset with the exact data needed in the report.

The XML was downloaded by te C# Class. The data was collected, filtered and sent to the RDL as a Dataset.

The final Method went like this:

private MemoryStream RenderPDFFromXML(Stream RDL, Stream XML)
{
    Warning[] warnings = null;
    string[] streamIds = null;
    string mimeType = string.Empty;
    string encoding = string.Empty;
    string extension = string.Empty;
    string filetype = string.Empty;

    ReportViewer RPT = new ReportViewer();
    RPT.ProcessingMode = ProcessingMode.Local;
    RPT.LocalReport.LoadReportDefinition(RDL);

    DataSet DS = new DataSet();
    XML.Position = 0;
    DS.ReadXml(XML);    
    ReportDataSource RDS;
    foreach (DataTable DT in DS.Tables)
    {
        RDS = new ReportDataSource(DT.TableName, DT);
        RPT.LocalReport.DataSources.Add(RDS);
    }
    RPT.LocalReport.Refresh();
    byte[] Bytes = RPT.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
    return new MemoryStream(Bytes);
}
1

1 Answers

0
votes

For me, I was passing wrong dataset name which was mentioned in .rdl file. For example

<DataSet Name="TestAbc"> </DataSet>

from model I was adding

new ReportDataSource("Abc", listOfModel),

You have to pass same name which you mentioned in .rdl file and which you passing as data source Name.