SSRS Reporting services .Net 5

Spread the love

In this post I will show you how to create SSRS reporting services builder with .NET core and .NET 5. The report contains different objects for example table and pie chart in addition to column chart.

Create the project

First open Visual studio 2019 and second create a C# console application

Create a C# console application
Create a C# console application
Target Framework .net 5.0
Target Framework .net 5.0

 As a result the Project is created

Project created
SSRSDemoApp project created

Install Package

Now first we need to install ReportViewerCore.NETCore, next press Tools -> NuGet Package Manager -> Package Manager Console and after that we need to install the package.

Install ReportViewerCore.NETCore
Install ReportViewerCore.NETCore
Furthermore go to this link and get the latest package link to install it.
Install-Package ReportViewerCore.NETCore -Version 15.1.14
Install Package
Install Package

Import the classes and the report

Once the package is installed, afterward Copy/Paste the following classes, Program.cs, Report.cs, ReportItem.cs and finally Report.rdlc into your project. Download the files.

Project
Project loaded

Program.cs is just a console that receives basic input from user, for example the type of the generated report (pdf, word, excel, html), as well as the directory where the report will be downloaded, in addition to the generated report file name etc..

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Reporting.NETCore;

namespace ReportingServicesDemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
                List<string> TypeDocList = new List<string>();
                TypeDocList.Add("pdf");
                TypeDocList.Add("html");
                TypeDocList.Add("docx");
                TypeDocList.Add("xlsx");

                Console.WriteLine("Type of document (pdf/html/docx/xlsx)");
                string TypeDoc = Console.ReadLine();
                if (!TypeDocList.Contains(TypeDoc))
                {
                    Console.WriteLine("Type should be from this list [pdf html docx xlsx] ");
                    Console.WriteLine("Press ENTER to exit...");
                    Environment.Exit(0);
                }

                Console.WriteLine("Folder location (Ex. C:/FolderName)");
                string FolderLoc = Console.ReadLine();
                if (!Directory.Exists(FolderLoc))
                {
                    Console.WriteLine("Folder location should be valid; Ex. C:/FolderName ");
                    Console.WriteLine("Press ENTER to exit...");
                    Environment.Exit(0);
                }

                Console.WriteLine("File Name");
                string FileName = Console.ReadLine();
                if (FileName == null || FileName.Trim().Equals(""))
                {
                    Console.WriteLine("File name should not be empty");
                    Console.WriteLine("Press ENTER to exit...");
                    Environment.Exit(0);
                }

                string renderFormat = "";
                string mime = "";

                switch (TypeDoc)
                {
                    case "pdf":
                        renderFormat = "PDF";
                        mime = "application/pdf";
                        break;
                    case "html":
                        renderFormat = "HTML5";
                        mime = "text/html";
                        break;
                    case "docx":
                        renderFormat = "WORDOPENXML";
                        mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                        break;
                    case "xlsx":
                        renderFormat = "EXCELOPENXML";
                        mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        break;
                }


                var report = new LocalReport();
                Report.Load(report);
                var DocumentType = report.Render(renderFormat);
                File.WriteAllBytes(FolderLoc + "/" + FileName + "." + TypeDoc, DocumentType);

                Console.WriteLine("Report generated Successfully");
        }
    }
}

Similarly Report.cs is a class which has the role to populate the data coming from different resources, mainly from database, and binds this data to the Items DataSet existing in Report.rdlc. For instance in this example, we populated the data manually to the variable items, and after that the items variable is bound to the Report DataSet. Notice also that we send to the report some parameters (Title and DateCreation).

using Microsoft.Reporting.NETCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;


class Report
{
    public static void Load(LocalReport report)
    {
        var items = new[] { new ReportItem { Code = "1122", Description = "Widget 6000", Price = 1.71m, Qty = 1 },
                            new ReportItem { Code = "1123", Description = "Gizmo MAX 1", Price = 1.41m, Qty = 5 } ,
                            new ReportItem { Code = "1124", Description = "Gizmo MAX 2", Price = 1.21m, Qty = 3 },
                            new ReportItem { Code = "1125", Description = "Gizmo MAX 3", Price = 1.31m, Qty = 6 },
                            new ReportItem { Code = "1126", Description = "Gizmo MAX 4", Price = 1.44m, Qty = 7 },
                            new ReportItem { Code = "1122", Description = "Widget 6000", Price = 1.71m, Qty = 2 },
                            new ReportItem { Code = "1122", Description = "Widget 6000", Price = 1.71m, Qty = 3 },
                            new ReportItem { Code = "1123", Description = "Gizmo MAX 1", Price = 1.41m, Qty = 3 },
                            new ReportItem { Code = "1127", Description = "Gizmo MAX 5", Price = 1.221m, Qty = 5 },
                            new ReportItem { Code = "1128", Description = "Gizmo MAX 6", Price = 1.231m, Qty = 5 }
        };

        var parameters = new[] { new ReportParameter("Title", "Items Invoice"), new ReportParameter("DateCreation", DateTime.UtcNow.ToString()), new ReportParameter("CreatedBy", "Joe Chalhoub") };
        using var fs = new FileStream("Report.rdlc", FileMode.Open);
        report.LoadReportDefinition(fs);
        report.DataSources.Add(new ReportDataSource("Items", items));
        report.SetParameters(parameters);
    }
}

ReportItem.cs contains the model class that will hold the report data.

using System;
using System.Collections.Generic;
using System.Text;


public class ReportItem
{
    public string Code { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public int Qty { get; set; }
    public decimal Total => Price * Qty;
}

Report.rdlc is the report, containing different types of objects, not only table chart but also pie chart and column chart, moreover if you look to the ReportData, inside the Datasets, you will see a dataset called Items, in fact this DataSet should have the same attributes as the ReportItem class.

Report.rdlc
Report.rdlc

Before we compile be sure to make the build action of report.rdlc to be Embedded resource

Report.rdlc Embedded resource
Report.rdlc Embedded resource

Run the report

Now first let’s compile, second we run and finally we see the report result.

Console Application
Console Application

Once the App is running, provide the following data

Type of Document : pdf
Folder Location : C:/Some Folder on your C drive
File Name : Test
And press Enter

If everything goes fine, as a result the report will be generated in addition to be downloaded directly to your folder.

Generated Report

Generated Report
Generated Report

Finally you can download this SSRS reporting services tool demo and all the classes to your .NET 5 visual studio 2019, not only-but also you can test it, and finally for any question please feel free to write me in this post.