Unable to render Report into PDF in Windows Server 2012 - c#

I'm facing one of the most strangest technical problem in .NET. I've written following logic to export report to PDF which is working in all the systems except Windows Server 2012. What could be the problem? I tried using the latest Microsoft.ReportViewer.Winforms.dll, but that too didn't help. Please help me if someone has any idea to deal with this.
reportViewer = new ReportViewer();
Warning[] warnings;
string[] streamids;
string mimeType, encoding, filenameExtension;
Assembly assembly = Assembly.LoadFrom(report.AssemblyName);
Stream stream = assembly.GetManifestResourceStream(report.ReportName);
reportViewer.LocalReport.LoadReportDefinition(stream);
//reportViewer.LocalReport.DataSources.Clear();
//reportViewer.ProcessingMode = ProcessingMode.Local;
//lr = reportViewer.LocalReport;
//lr.ReportPath = "Reports\\CommentReport.rdlc";
reportViewer.LocalReport.DataSources.Clear();
foreach (var dataSource in report.DataSources)
{
reportViewer.LocalReport.DataSources.Add(new ReportDataSource(dataSource.Key, dataSource.Value));
}
pdfByteArray = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

Related

Can't render chart in RDLC report

I'm using RDLC file to render a report (without SQL Server Reporting Services) and return it from my controller as a file. This is part of my MVC web application.
public ActionResult Index()
{
var report = new LocalReport();
report.ReportPath = Path.Combine(Server.MapPath("~/Reports"), "Report1.rdlc");
var reportData = new List<MonthlyData> {
new MonthlyData {RecordNo=1, Tid="123456", Active=10, Inactive=1}
};
ReportDataSource rd = new ReportDataSource("DataSet1", reportData);
report.DataSources.Add(rd);
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>" + "PDF" + "</OutputFormat>" +
//" <PageWidth>8.5in</PageWidth>" +
//" <PageHeight>11in</PageHeight>" +
//" <MagroupinTop>0.5in</MagroupinTop>" +
//" <MagroupinLeft>1in</MagroupinLeft>" +
//" <MagroupinRight>1in</MagroupinRight>" +
//" <MagroupinBottom>0.5in</MagroupinBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = report.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
return File(renderedBytes, mimeType);
}
This is the result:
table example in rdlc file
Everything works like a charm until I decide to add a chart.
rdlc file with chart
Now when I render it I get two exceptions:
Microsoft.Reporting.WebForms.LocalProcessingException: „An error occurred during local report processing.”
DefinitionInvalidException: The definition of the report 'C:\Users\agutowski\Documents\Visual Studio 2017\Projects\rdlcMvc\rdlcMvc\Reports\Report1.rdlc' is invalid.
and
Microsoft.Reporting.WebForms.LocalProcessingException: „An error occurred during local report processing.”
ReportProcessingException: The definition of this report is not valid or supported by this version of Reporting Services. The report definition may have been created with a later version of Reporting Services, or contain content that is not well-formed or not valid based on Reporting Services schemas. Details: The report definition has an invalid target namespace 'http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition' which cannot be upgraded.
I already tried different versions of Microsoft.ReportViewer.Common and Microsoft.ReportViewer.WebForms.
I solved it by using Microsoft.ReportingServices.ReportViewerControl.WebForms and Microsoft.SqlServer.Types NuGet packages instead of Microsoft.ReportViewer.Common and Microsoft.ReportViewer.WebForms.

Execute SSRS Report from C# save as PDF

So, here's my delema.
The title says it all. I cannot seem to find any guidance on how to execute a SSRS report remotely and save it as a PDF.
I have tried to follow the below article.
Using Reporting Services (SSRS) as a reference in an ASP.NET Core site
However, when I add the Service Reference to my project some of the methods seem to have the wrong signatures.
For example.
rsExec.LoadReportAsync(report, null);
in my reference the first parameter is a TrustedUserHeader object.
Does anyone have a good starting point on how to execute an SSRS report from C#? I cannot find any simple example.
I do this by using Microsoft.Reporting.Webforms and the following method:
using Microsoft.Reporting.WebForms;
...
public byte[] ExportToExcel(string reportName, string[] paramNames, string[][] paramDic)
{
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType;
string encoding;
string extension;
ReportViewer rv = new ReportViewer { ProcessingMode = ProcessingMode.Remote };
rv.AsyncRendering = false;
ServerReport sr = rv.ServerReport;
sr.ReportServerUrl = new Uri("http://<server>/reportserver");
sr.ReportPath = "/<report path>/" + reportName;
if (paramNames.Length != 0)
{
List<ReportParameter> paramList = paramNames.Select((t, i) => new ReportParameter(t, paramDic[i])).ToList();
rv.ServerReport.SetParameters(paramList);
}
return rv.ServerReport.Render("Excel", null, out mimeType, out encoding, out extension,
out streamIds, out warnings);
}
The byte array can then be sent to the client via Response or saved to a file to be emailed/transferred later.
The first parameter is the name of the report, the second is an array of parameter names, and the third is an array of arrays containing the parameter values. I wrote this method early in my career and I wouldn't write it this way now. If you use this, I would refactor the code to take two parameters: reportName and a Dictionary called parameters or something like that to manage the parameter values.

SSRS Render Multiple Pages from Report in C#

I have a service that renders a report from SSRS which works perfectly fine with one page, however when I tried to change the code to render multiple pages separately I can't seem to get it to work as expected.
I used a guide on the MSDN blogs (http://blogs.msdn.com/b/bryanke/archive/2004/02/11/71491.aspx#code) to try and achieve this, but my StreamIDs doesn't seem to be working as I expected.
Here's my code from the Render() method onwards (the rest seems to be okay, but I can provide on request):
var firstPage = rsExec.Render(format.ToString(), deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);
var numberOfPages = streamIDs.Length + 1;
results = new Byte[numberOfPages][];
results[0] = firstPage;
if (numberOfPages > 1)
{
for (int i = 1; i < numberOfPages; i++)
{
deviceInfo = $#"
<DeviceInfo>
<OutputFormat>JPEG</OutputFormat>
<StartPage>{i + 1}</StartPage>
</DeviceInfo>";
results[i] = rsExec.Render(format.ToString(), deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);
}
}
This generates the report okay, but I expected the streamIDs to become the number of extra pages required, but it always only has one entry in it. Am I doing something stupidly wrong here?
I'm using SQL Server 2008 R2.

Search for available reports on server using C#

I would like to somehow cycle through my Reporting Server and display available reports to the user. Is this possible?
My Code is as follows (still in development):
ServerReport sr = new ServerReport();
sr.ReportPath = reportViewer1.ServerReport.ReportPath;
sr.ReportServerUrl = new Uri(Path);
ReportParameterInfoCollection rpc = sr.GetParameters();
if (rpc.Count > 0)
Console.WriteLine("New");
string outputPath = #"C:\Temp\PdfReport.pdf";
string mimeType;
string encoding;
string extension;
string[] streams;
Warning[] warnings;
byte[] pdfBytes= sr.Render("PDF", string.Empty, out mimeType,
out encoding, out extension, out streams, out warnings);
// save the file
using (FileStream fs = new FileStream(outputPath, FileMode.Create))
{
fs.Write(pdfBytes, 0, pdfBytes.Length);
fs.Close();
}
I use the ReportingService2010 web service to retrieve all deployed items (data sources, shared data sets and reports) and also to deploy directly to Reporting Services.
Here is just one example of the many methods available:
http://msdn.microsoft.com/en-us/library/reportservice2010.reportingservice2010.listchildren.aspx
The ListChildren method will return all items (as a CatalogItem object) under a folder. If you give it the root folder, it will return everything. You should then be able to determine what item each CatalogItem represents.
Hope that helps,
Ash
For anyone else having this problem and would like more information about Ash Shah's answer, see this SO post:
How do I get a list of the reports available on a reporting services instance

Export a parameterized SSRS report from C# code

Currently I have SQL Reporting Services 2005 set up, with the report manager at a URL on which users can access reports. The reports are working great there.
My issue is trying to generate these reports in C# .net 4.0 code without any user interaction (such as using the report viewer on screen). I would like to generate and export a report to a PDF file in a C# .net application. The reports have required parameters so I would need to pass the parameters to the report. How can I do this?
I have been searching around online, and either I'm using the wrong keywords or there isn't much information on this. I am quite amazed at how difficult it has been to find information on this, as I would expect it to be a fairly common question. Any and all advice / help is appreciated.
I've not used the 2005 version of the ReportViewer much. But you should be able to do something like this:
ServerReport serverReport = new ServerReport();
serverReport.ReportPath = "path/to/report";
serverReport.ReportServerCredentials = ...;
serverReport.ReportServerUrl = "http://....";
serverReport.SetParameters(...);
string mimeType;
string encoding;
string extension;
string[] streams;
Warning[] warnings;
byte[] asPdf = serverReport.Render("PDF", string.Empty, out mimeType, out encoding, out extension, out streams, out warnings);
The general takeaway being that ServerReport and LocalReport were both designed to be usable outside of a ReportViewer.
string outputPath = "C:\Temp\PdfReport.pdf";
ReportViewer reportViewer = new ReportViewer();
reportViewer.ServerReport serverReport = new ServerReport();
reportViewer.ServerReport.ReportPath = #"path/to/report";
reportViewer.ServerReport.ReportServerUrl = new Uri(#"http://...");
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.ServerReport.ReportServerCredentials.NetworkCredentials = new
System.Net.NetworkCredential(username, password, domain)
List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("parameterName", "value"));
string mimeType;
string encoding;
string extension;
string[] streams;
Warning[] warnings;
byte[] pdfBytes= serverReport.Render("PDF", string.Empty, out mimeType,
out encoding, out extension, out streams, out warnings);
// save the file
using (FileStream fs = new FileStream(outputPath, FileMode.Create))
{
fs.Write(pdfBytes, 0, pdfBytes.Length);
fs.Close();
}
I had a similar issue where I wanted to open the report as a PDF. If you just need to open a pdf with parameters in a browser window then you can use the report server itself and specify Format=PDF as a querystring option.
Example:
http://myServer/ReportServer/Pages/ReportViewer.aspx?%2fMyApplicationReports%2fAcutalReportFileName&rs:Command=Render&rs:Format=PDF&ParamOneId=31943&ParamTwoDate=17072015
I hope this saves someone else out there some time!

Categories