Introduction:
In this article we will see how we can integrate SSRS reports into Asp.Net core web Application.
Reporting was somewhat a feature lacking in MVC core for a considerable amount of time but Microsoft has overcome this issue since the release of Asp.Net core version 2.1.
Lets see an example of how to create and display RDLC report in Asp.Net Core using visual studio 2017.
First up creating a new Asp.Net Core Project in Visual Studio 2017.
From the left Sidebar choose .NET Core and select Asp.Net Core Application
We have named the Project as "RdlcReportsIntegration".
using Visual Studio 2017, Select Asp.Net Core version 2.1 and select the option
Web Application (Model-view-Controller) and click OK.
Next up right click on Controllers folder and Select MVC Controller- Empty
Name the controller as Reports Controller
From Solution Explorer on the right, right click your Asp.Net core project and Select the option Manage Nuget Packages and browse for this Nuget package AspNetCore.Reporting and click on the install button Or go to package manager console and type "Install-Package AspNetCore.Reporting -Version 2.1.0" and hit enter.
Then right click you solution and add a new Project
From the left side bar select Windows Desktop and choose WindowsFormApp, We are creating a new Windows Forms App in order to create RDLC reports, We have named our WindowsFormApp as RdlcReports and then click OK.
Then right click the Windows Application and then choose Add and New Item
Here search for report in the top right section and select Report from the list for in order to Create RDLC reports, we have named our Report as Employees.rdlc.
Note: if you do not see the Report option in the list then go to extensions and updates and install the required extension for RDLC reports in visual studio 2017.
Now since an empty RDLC report has been created, we are going to populate the RDLC report with some data.
So, right click your Windows Form Application Project and choose Add and New Item, click on Data and select ADO .NET Entity Data Model.
we have named our ADO.Net Entity Data Model as Model.
Then choose the option Code First from Database from the Model Contents and hit Next
So, we created a Database and named it as Employee Db in SQL Server Express and there we created a table by the name of employee in order to populate the RDLC report with some dummy data.
Now to configure the database from the as the data source from ADO.Net Entity Model named as Model from the previous step.
We will provide the Server name and choose the Database as EmployeeDb from the list.
We will name our connection Sting as RDLCReportingTest and hit Next.
Now here to choose the required database object which in our case is the table being named as employees, checked the table option and hit Finish.
So as a result we are going to get a POCO class named as employee.cs in our Windows Forms Application which we called as RdlcReports.
In the next step copy and paste the required file employee.cs into the Models folder of the Asp.Net Core Application RdlcReportsIntegration and changed the namespace from RdlcReports to RdlcReportsIntegration as.
Now click on your RDLC report which is Employees.rdlc and click on New at top left corner and select DataSet.
Then select object from the Data Source Type and click Next,
Next Select the the RdlcReport, which is name of the Windows Application Project and select the code first generated class which in our case is named as employee and hit Finish
Now, here we have named our dataset as empDataSet, this is the name we are going to provide in our Action Method later on in order to populate the Rdlc Report.
Next up in the Rdlc Report, Right click add a header which is the report header and footer as the report footer respectively, then add a new Table by right click on the Details Section and insert Table.
Next Up we created a Parameter by Right clicking Parameters section at the top left section of our report designer and named as Param, we are taking it as the report header, and we will pass its corresponding value from our Reports Controller's Action Method as a Parameter.
Next Drag and drop each corresponding column from the Dataset onto the details section of our RDLC Report and change their colors and background colors accordingly.
Next we created a Report heading and sub heading and a Text box, which is for the Current Date, we have taken it as another Parameter named as currentDate and we will also pass its corresponding value from the Report Controller later on.
So at this point the designing part of our RDLC report has been completed,
Next Up we will add a few Nuget Packages into our Asp.Net Core Web application, so right click on your Asp.Net Core web application and select the option Manage Nuget Packages.
Here Search for this Nuget Package System.CodeDom and click Install.
Note: We are using Dapper as the ORM in order to fetch the data from the Database.
Next Up Install another Nuget Package called as System.Drawing,Common from the Manage Nuget Packages list.
Next Up Install another Nuget Package which is the last of Nuget Packages required for this Particular article and that is System.Data.SqlClient.
So, we are done with the installation of all the Nuget Packages, right click the web application and select Clean and Rebuild so that all the Nuget Packages will be restored.
Now in wwwroot folder of our web application, right click, create a New Folder as Reports and copy and paste the RDLC report which is Employees.rdlc from the Windows Form Application Project.
Then right click the report go to properties and change the Build Action Property from embedded resource to Content and Copy to output Directory from Do not Copy to Copy Always.
Next up go to the appsettings.json from the MVC core project and add the required code for placing the connection string in the JSON format.
Next to add a DB Context class into the Models folder in order to communicate with the Database, by right clicking the Models folder in the web application, add New Item and select class and we have named our class as AppDbContext.cs.
So, we are not inheriting the AppDbContext with application DB Context as we so often does in Asp.Net Core, We are just declaring a public connection property which we will later consume from the startup.cs using the Dependency Injection.
Next up we will right click the web application and create a New folder as Repositories since we are using the repository pattern for communicating with the Database,
In this Repositories folder, we will create an Interface called IEmployeeRepository.cs and a mock class called EmployeeRepository.cs
In our Interface, we are creating a Method GetEmployees for getting all the employees Data,
and the Interface will be Implemented by the class called EmployeeRepository, which is the mock class getting the required data from the database.
In this class we are implementing the method GetEmployees defined in the IEmployeeRepository Interface and using the dapper ORM, we are fetching the Data from the database,
sp_EmployeesList is the store procedure we created in the SQL server express.
Next up make a few changes into the program.cs of your web application by defining and using the content root and web root methods for identifying the root directory.
Next up go to the Startup.cs and get the Connection string by using the Dependency Injection inside the configure Services Method.
Getting the connection String from appsettings.JSON file by the key called Default Connection.
Next is to Inject the Interface and the Mock Repository class we created in the Repository folder using the same Dependency Injection Principle along with adding services.AddOptions() method in the startup.cs Configure Services Method.
Now we are done with all the code except the actual code we are going to do in our Reports controller, so inside our Report Controller, we will create an Action Method called PrintReport(),
We use it for displaying our RDLC report in the PDF format.
In our Reports Controller, first up Injecting the ILogger, IHostingEnvironment and the IEmployeeRepository inside the Constructor.
Note: system.Text.CodePagesEncoding is used for Unicode encodings in Asp.Net Core, In case of any error while executing this line, We would required to Install an additional Nuget Package called System.Text.Encoding.CodePages , for which go to the package Manager console and enter
Install-Package System.Text.Encoding.CodePages -Version 5.0.0
Next up is to setup the default route for our application, and that we will do in the configure Method of our startup.cs Class.
In the code above, we are directing to the root directory wwwroot of our Web Application and we have placed the required RDLC report in Reports folder, which its self is present inside the wwwroot folder.
we are using Dictionary for a key value pair, since we are passing two parameters to the RDLC report, earlier we did mention we will be passing two parameters named as param and the currentDate, In param we are passing the Report Heading and currentDate is the present date which we wanted to display at the top of the RDLC report.
employeeRepository is the object for the interface IEmployeeRepository and we are using the repository pattern to call the abstract method call GetEmployee.
extension variable is the page Index which gets called at the time of report rendering,
empDataSet is the dataset name we declared while designing our RDLC report.
At last we will clean and rebuild our MVC Core Project and then press ctrl+f5 to run the project to see the desired report.