Execute SSRS Report from C# save as PDF - c#

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.

Related

How to change the database in an SSRS report at run-time

I am attempting to produce a c# program to run an SSRS report on any one of a number of identical databases, the target database to be specified at run-time. To this end, I create a solution and project, and in this project include an SSRS report. This report has a dataset LegislationData which invokes a stored procedure in a specimen database.
I am now trying to get this to run in the C# project. I create a form with a report viewer and a go button and attempt to set up the report. I envisaged some code along the following lines:-
MyReport my_report = new MyReport();
my_report.ConnectionString = "blah blah"; // or
my_report.DataSet.ConnectionString = "blah blah"; // or
my_report.LegislationData.ConnectionString = "blah blah"
and then
report_viewer.Report = my_report; // or
report_viewer.LocalReport = my_report; // or
report_viewer.SetReport(my_report);
but none of these things actually exist.
Can someone explain to me very slowly and in words of one syllable what I need to do here? I have looked at the answers to similar questions here and here but to be frank the answers make no sense.
The first thing you need to realise is that SSRS has to be added into your C# application as a web reference. There's a guide on how to do this here: https://msdn.microsoft.com/en-gb/library/ms169926.aspx. Basically it sounds worse than it is, and it should only take a few minutes to configure all this. I found that MSDN link was corrupted for me, so here's another place that discusses how to do this: https://sqluninterrupted.com/2012/03/04/adding-a-reporting-services-web-reference-to-net-application/.
Once you have your report running from a C# application you will need to decide what you want to do with the output, convert it to PDF, stream it to the screen, save it as Excel, etc.
I haven't done this before, but it looks as though you can embed a data source into your report that uses an expression based on a parameter. So you would pass in a parameter to run the report that would be a connection string. You would also need to pass in any other parameters you might have in your report.
So step 1 add the web reference for SSRS.
Step 2 add some code to run your report, e.g. here's an example that returns the report as a byte array in PDF format:
public byte[] RenderReport(ReportExecutionService rs, string reportName, int variant)
{
Console.WriteLine("Rendering " + reportName + "_" + variant.ToString("00"));
byte[] result = null;
string reportPath = "/Prototypes/Inheritance Letters/" + reportName;
const string format = "PDF";
const string devInfo = #"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
//Prepare report parameters
var parameters = new ParameterValue[2];
parameters[0] = new ParameterValue { Name = "row_id", Value = variant.ToString() };
parameters[1] = new ParameterValue { Name = "bulk_run", Value = "1" };
rs.ExecutionHeaderValue = new ExecutionHeader();
rs.LoadReport(reportPath, null);
rs.SetExecutionParameters(parameters, "en-gb");
try
{
string encoding;
string mimeType;
string extension;
Warning[] warnings;
string[] streamIDs;
result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
rs.GetExecutionInfo();
return result;
}
catch (SoapException e)
{
Console.WriteLine(e.Detail.OuterXml);
return null;
}
}
Step 3 pass the connection string as one of the parameters, and use an expression in an embedded data source in your report to pick this up and use it.
Step 4 decide what to do with the rendered output. For example, here I render a report then save the output to a PDF:
byte[] result = new Render().RenderReport(rs, "ACQ_Welcome_Letter", i);
new Render().CreatePDF(i, "Welcome Letter", "ACQ_Welcome_Letter" + "_" + fuelType, result);
Here's the CreatePDF method, it has a lot of other garbage in for my particular solution, but it gives you a taste of how to do this:
public string CreatePDF(int variant, string subFolder, string reportName, byte[] result)
{
//We want 16 variants, but we pass in a number from 1 to 48, so use modulo division to convert this back to a 1 to 16 range
variant = (variant - 1) % 16 + 1;
Console.WriteLine("Saving " + reportName + "_Variant_" + variant.ToString("00") + ".pdf");
try
{
//Determine the target folder/ filename for the PDF
//Snail Mail has its own folder, all PDFs go into that folder and then are manually processed
string folder = #"S:\Change Management Documents\SMETS1\Inheritance Comms\" + subFolder + #"\";
string filename = reportName + "_Variant_" + variant.ToString("00") + ".pdf";
//Remove any existing content
string[] filePaths = Directory.GetFiles(folder, filename);
foreach (string filePath in filePaths)
File.Delete(filePath);
//Now save the PDF
string path = folder + #"\" + filename;
FileStream stream = File.Create(path, result.Length);
stream.Write(result, 0, result.Length);
stream.Close();
return filename;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "";
}
}

Dynamic images and databinding

Imagine having a dataset with data which binds to the report - works just fine.
Now, I wish to add into another dataset dynamically or dynamic parameters. These parameters, or dataset, will contain images. The images are in a byte array.
I am unable to get them to display in the RDLC report when generated.
This is what I have done so far:
Obtain the byte[] array, in code, from an external source (a URL)
Convert the byte[] array of the image to Base64String
Add this as a ReportParameter
Then in the RDLC, I added this parameter and then added an image control.
Then for this image box/control, for the value I set it to the following:
=System.Convert.FromBase64String(Parameters!TheImage.Value)
However I just get rendered an "X" for the image as if the image was not found but it definitely is there.
The code to render the report is more or less the following (only pasted import bits):
using (var rv = new ReportViewer())
{
rv.ProcessingMode = ProcessingMode.Local;
rv.Reset();
using (var sr = new System.IO.StreamReader(#"C:\MyReport.rdlc"))
{
rv.LocalReport.LoadReportDefinition(sr);
rv.LocalReport.EnableExternalImages = true;
var
reportParameters = new List<ReportParameters>();
reportParameters.Add(new ReportParameter("TheImage", Convert.ToBase64String(TheExternalImage.Image);
rv.LocalReport.SetParameters(reportParameters);
string mimeType;
string encoding;
string fileExtension;
string[] streamIds;
Warning[] warnings;
var streambytes = rv.LocalReport.Render(format.ToString(), null, out mimeType, out encoding, out fileExtension, out streamIds, out warnings);
rv.LocalReport.ReleaseSandboxAppDomain();
}
}
Any ideas where I am going wrong?
to do this convert your image byte array into a base 64 string and pass that string into the parameter (you are already doing this). Then set the image to have it's source as Database and make sure you set the MIME type correctly on the image field then use the parameter as the source.
Most likely it is setting the image source to Database that you are missing.

Is there any way to ask a ReportViewer if the result of the query inside a Report will yield more than 65k rows?

I have my reports in a Reporting Services server, inside my .rdl there is a query that accepts parameters. I pass those parameters with an instance of ReportViewer. I have a method that downloads the result of the report in Excel format without using the ReportViewer directly. The method is the following:
private void CreateEXCEL(Dictionary<string, string> parametros, string nombreReporte)
{
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
// Setup the report viewer object and get the array of bytes
string ReportServerURL = ConfigurationManager.AppSettings["ReportServerCompletitudURL"];
string ReportName = ConfigurationManager.AppSettings["ReportNameRankingVentaPDV"] + "/" + nombreReporte;
MyReportViewer.Reset();
MyReportViewer.ProcessingMode = ProcessingMode.Remote;
MyReportViewer.ServerReport.ReportPath = ReportName;
MyReportViewer.ServerReport.ReportServerUrl = new Uri(ReportServerURL);
List<ReportParameter> parameters = new List<ReportParameter>();
foreach (var d in parametros)
{
parameters.Add(new ReportParameter(d.Key, d.Value));
}
MyReportViewer.ServerReport.SetParameters(parameters);
byte[] bytes = MyReportViewer.ServerReport.Render("EXCEL", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + nombreReporte + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download
}
Now the idea is that I can't create a file with more that 65536 rows as an Excel file, the idea is to "Ask" if the result of the query inside the Report will yield more than 65k rows, then use csv format.
I dont see that reportviewer server control have a method that checks the result of the query.
I don't want to use pagebreaks inside the SSRS reports. Is there any way to ask in my code behind?
Not sure if this helps but this is a work around for exporting to excel.
Create a parent group on the tablix (or table, or list) and in the Group on: field enter the expression below.
Add Page break between each instance of a group
=CInt(Ceiling(RowNumber(nothing)/65000))
See Question on Here.
I found the solution to this particular problem like this:
Put this expression in my "Details" Group. In Disabled property: =IIF(rownumber(nothing) mod 10000=0,false,true) BreakLocation: End.
After this change, I can save this excel divided in different worksheets in the same excel sheet for every 10k rows. I tried doing the ceiling but if you have a rownumber expression inside that group it wont work.

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