In many applications, there is often a need to print out data. Despite the advancements in technology, the requirement to print documents still persists. This article will discuss a solution for printing data from a .NET application using SQL Server Reporting Services (SSRS) reports.
The Solution: SSRS Reports in PDF Format
When it comes to printing data, one of the main concerns is ensuring that it will print correctly on any printer. One way to address this issue is by using PDF format, as what you see on the screen is usually what will be printed out. In this case, we will utilize SSRS reports in RDL format, which can easily be exported to PDF for printing.
To implement this solution, we will create a Reporting Services report (RDL format) and then render it as a PDF directly in the .NET application. This can be achieved by using the Reporting Services .NET DLL in your .NET Winforms project, which allows you to attach data and render the local report as a PDF.
The Process
Here are the steps to implement this solution:
- Create a Reporting Services report in RDL format. If you are not familiar with this, there are plenty of resources available to learn about it.
- Create a new .NET Windows Forms application (this code also works in WPF and Universal apps).
- Add the Reporting Services NuGet package to your project.
- Load the RDL file in your .NET code using the Microsoft.Reporting.WebForms.LocalReport class.
- Set the data source for the report using a DataTable.
- Render the report as a PDF using the Render method.
- Save the PDF file and display it.
Here is an example of the .NET code:
// Load the RDL file
Microsoft.Reporting.WebForms.LocalReport report = new Microsoft.Reporting.WebForms.LocalReport();
report.ReportPath = @"c:\temp\RDLExample.rdl";
// Set the data source
System.Data.DataTable tbl = scheduleData.Tables[0];
Microsoft.Reporting.WebForms.ReportDataSource rds = new Microsoft.Reporting.WebForms.ReportDataSource();
rds.Name = "dsSchedule";
rds.Value = tbl;
report.DataSources.Add(rds);
// Set default values for report parameters
report.SetParameters(new List<Microsoft.Reporting.WebForms.ReportParameter> {
new Microsoft.Reporting.WebForms.ReportParameter("PlantName","YourPlant"),
new Microsoft.Reporting.WebForms.ReportParameter("ServerName", "Server"),
new Microsoft.Reporting.WebForms.ReportParameter("DatabaseName", "DB"),
new Microsoft.Reporting.WebForms.ReportParameter("ConnectionString", "ConnStr")
});
// Render the report as a PDF
Byte[] mybytes = report.Render("PDF");
// Save and display the PDF file
if (mybytes != null && mybytes.Length > 0)
{
using (FileStream fs = File.Create(@"c:\temp\test.pdf"))
{
fs.Write(mybytes, 0, mybytes.Length);
}
System.Diagnostics.Process.Start(@"c:\temp\test.pdf");
}
Example
In the example application, you can click the “Get Data” button to retrieve data, and then click the “Print Local” button to see the RDL PDF file. There is also a “Write RDL File to DB” button that stores the local RDL file in a database table. The “Print from RDL DB” button uses the RDL file stored in the database.
It is worth mentioning that RDL files can be stored in a Varbinary(max) column in a table. This is similar to how the Reporting Services server stores RDL files in the ReportServer database. In some cases, you may want to copy the RDL file from the central Reporting Services server to a local database in remote plants. This ensures that the report still works even if the WAN connection goes down.
Conclusion
This article demonstrated how to use SSRS reports in RDL format and render them directly from a .NET Winforms application. By utilizing this approach, you can print data from your application without being tied to a Reporting Services instance. We hope you found this article helpful for your printing needs.