If you are one of the people who used and enjoyed SQL Server Reporting Services (SSRS) in SQL 2000 and you wanted to use it in your windows/web applications without the server side components (just like what you are doing with Crystal Reports). So you would be much exited about the new ReportViewer Controls. ReportViewer controls are two controls for Windows and Web applications that can either show reports produced by SQL Server Reporting Services or process and render reports locally.
These reports are called Local Report and it's intended to provide reporting features without SSRS. You can use them without having a license for SSRS but be aware that you would lose some features like
I used ReportViewer for Windows forms in small report in which I had to add custom code in it and I had some problems that I would like to share with you solutions for them.
First we would go quickly through the process of creating simple report as following:
1) Create a new windows forms project and open its form. Drag the ReportViewer control from the Data tab as shown in the image below
2) Use the smart tag tasks to dock the report in all the form (you can use properties window as well). Then from the smart tag choose "Design new report". If you didn't see this option you can simply create new report. From solution explorer, right click on the project name then choose "add new item" then choose report.
3) The report file would have the extension "rdlc" not "rdl" as it would be in server reports.
4) Local Reports need to have a data source so go to Data Sources window and create your data source (let's say that it will get all Category name and description from categories table in Northwind)
5) From the ToolBar window, drop a table inside the report
6) From the Data Source window, drag & drop the CategoryName and Description fields to the details row of the table. Now you finished creating the report. Let's bind it to the ReportViewer
7) Return back to the ReportViewer and from the smart tag menu choose, select your report name from the drop down menu.
8) Run the application.
Now you have the report. Let's go to add custom code to the report.
Why would you add custom code?
You may want to add custom code to the report to do more actions than what's provided with the report functions. So it gives you a base for extending your report. You may want to add custom code to implement simple string manipulation task or sophisticated data access manipulation.
Cod added to Local Report can be one of two types:
Embedded Code: Added directly inside the rdlc file. If you open the file in notepad, you can see the code inside the <Code> tag.
Custom Assemblies: You can add a reference in external assembly and use functions inside it.
How to add a custom code in a local report?
According to MSDN library, you add it as following:
What's the code that I can add?
You can add class members only (properties, methods, fields) but you can't add classes because these members are encapsulated in a class at runtime, this class called Code class.
Take a look at this code snippet.
Public ReadOnly Property getSomeData() As String
Dim sharedMember As String = "Omar"
Public Function toUpperCase(ByVal s As String)
Dim conn As New System.Data.SqlClient.SqlConnection()
You can use this code inside a textBox or a table column as following =Code.toUpperCase(Code.getSomeData)
What are the namespaces that I can use inside my custom code?
All .NET namespaces can be used inside your custom code but be aware that by default only Microsoft.VisualBasic, System.Convert, and System.Math namespaces are referenced inside your reporting custom code.
Now let's jump into the problems.
Adding code that uses types that are not in these namespaces (Microsoft.VisualBasic, System.Convert, and System.Math)
Let's say that you are using a code that access SQL Server database to get some data to use it inside your report. You are using SqlConnection in your code. You would receive an error like this
There is an error on line 9 of custom code: [BC30002] Type 'System.Data.SqlClient.SqlConnection' is not defined.
Because System.Data.SqlClient resides in the System.Data.dll assembly and this assembly is not referenced by default by the report custom code. To solve this problem you need to add a reference to the System.Data.dll assembly in the References tag in the report properties window.
But this is not the end of the story; you would face another problem which described in the next step
How to make an assembly trusted for your report code
Because Local Reports like SQL Server Reporting Services reports are written using open format (Report Definition Language (RDL)). Any user can add any code or reference any assembly from your rdl file at the production environment which would cause a BIG security hole. You need to mark these assemblies as trusted inside your Windows Application code. Or you would get an error like this
The report references the code Module 'System.Data, Version=188.8.131.52, Culture=neutral, Publickey Token=b77a5XXXXXXXX', which is not a trusted assembly
You need to use the AddTrustedCodeModuleInCurrentAppDomain method of the LocalReport class. You can add the following line at the Form_load event handler of your windows application.
this.reportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Data, Version=184.108.40.206, Culture=neutral, PublicKeyToken=b77a5c561934e089");
And again it's not the end of the story; you will face another problem ;). Please be patient with me because I faced all these problems sequentially and I didn't find any resources to help me.
What are the Code Access Security (CAS) permissions assigned to any code running inside LocalReport by default
By default the only permission bound to the code running inside the LocalReport is ExecutePermission So your code can't access file system, databases, registry… unless you give it explicitly the required permissions.
For example, if you are trying to connect to a database from your code, you will get this error
Request of the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=220.127.116.11, Culture=neutral, PublicKey Token=b77a5c561934e089' failed.
To give the assemblies running in the report the appropriate permissions, you need to use the ExecuteReportInCurrentAppDomain Method of the LocalReport class. Which causes the expressions in the report to be executed inside the current appDomain instead of executing them in a sandbox.
The ExecuteReportInCurrentAppDomain method takes one parameter of the type Evidence.
In the following example I'm using the evidence associated with my current appDomain so the report code will have all permissions that I have in my application
By working on these steps, your code will run smoothly inside your report.
For the people who are trying to move their code in separated assembly and reference it from their report, they would face another problem.
If you are following the steps mentioned in MSDN regarding how to reference custom code (mentioned above). You will end up with this message
Error while loading code module: ‘ClassLibrary1, Version=18.104.22.168, Culture=neutral, PublicKeyToken=null’. Details: Could not load file or assembly 'ClassLibrary1, Version=22.214.171.124, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
After a lot of trials with this error, I found that I have to do the following.
this.reportViewer1.LocalReport.ReportEmbeddedResource = "testLocalReport.Report1.rdlc";
this.reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
I hope this would help you to really enjoy VS 2005 features