Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
SQL Server 2016 Reporting Services Cookbook

You're reading from   SQL Server 2016 Reporting Services Cookbook Your one-stop guide to operational reporting and mobile dashboards using SSRS 2016

Arrow left icon
Product type Paperback
Published in Nov 2016
Publisher Packt
ISBN-13 9781786461810
Length 596 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Robert Cain Robert Cain
Author Profile Icon Robert Cain
Robert Cain
Dinesh Priyankara Dinesh Priyankara
Author Profile Icon Dinesh Priyankara
Dinesh Priyankara
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting It Ready – Configuring Reporting Services FREE CHAPTER 2. Authoring Reports with SQL Server Data Tools 3. Advanced Report Authoring with SQL Server Data Tools 4. Authoring Reports with Report Builder 5. Improving User Experience – New Designing and Visualization Enhancements 6. Authoring Reports with the Mobile Report Publisher 7. Consuming Reports – Report Access Enhancement 8. Reporting Solutions for BI – Integration 9. SharePoint Integration 10. Administering and Managing Reporting Services 11. Securing Reports in Reporting Services 12. Custom Programming and Integration to .NET Applications

Referencing external .NET assemblies

You have already seen the way of adding custom code to reports and you may have already experienced the issues with it such as:

  • It supports only Vb.NET
  • The same function has to be added to each and every report

With this recipe, we can overcome it by making an assembly that contains all required functions and use it with the report rather repeating it in every report.

Getting ready

In order to make an assembly with all required functions, you need to create a .NET class library using one of .NET languages. Yes, it is not limited to Vb.NET. Let's make one using C#.NET language.

Follow these steps for creating a class library:

  1. Open the Visual Studio and create a C#.NET Class Library project. Name it as StandardFunctions.
  2. Create a public class called Functions and add a public static method named GetTextColor:
          using System; 
          using System.Collections.Generic; 
          using System.Linq; 
          using System.Text; 
          using System.Threading.Tasks; 
          namespace StandardFunctions 
          { 
              public class Functions 
              { 
                  public static string GetTextColor(string messageType) 
                  { 
                      string color = "black"; 
                      switch (messageType) 
                      { 
                          case "Warning": 
                              color = "Yellow"; 
                              break; 
                          case "Error": 
                              color = "Red"; 
                              break; 
                          case "Success": 
                              color = "Green"; 
                              break; 
                      } 
                       return color; 
                  } 
              } 
          } 
    
  3. Open the AssemblyInfo.cs file in your project. Add System.Security namespace and make sure the AllowPartiallyTrustedCallers attribute is added:
          using System.Reflection; 
          using System.Runtime.CompilerServices; 
          using System.Runtime.InteropServices; 
          using System.Security; 
          [assembly: AssemblyTitle("StandardFunctions")] 
          [assembly: AssemblyDescription("")] 
          [assembly: AssemblyConfiguration("")] 
          [assembly: AssemblyCompany("")] 
          [assembly: AssemblyProduct("StandardFunctions")] 
          [assembly: AssemblyCopyright("Copyright ©  2016")] 
          [assembly: AssemblyTrademark("")] 
          [assembly: AssemblyCulture("")] 
          [assembly: AllowPartiallyTrustedCallers()] 
          [assembly: ComVisible(false)] 
          [assembly: Guid("4d89a812-f315-48f3-af97-a7576cc4060b")] 
          [assembly: AssemblyVersion("1.0.0.0")] 
          [assembly: AssemblyFileVersion("1.0.0.0")] 
    
  4. Get the project properties and open Application page. Make sure that the Assembly name is set as StandardFunctions.
  5. Open Signing* page of Project Properties and check Sign the assembly checkbox. This is for signing the assembly with a strong name as it is required when adding to GAC. Select <New...> from Choose a strong name key file drop down:

    Getting ready

    Figure 12.09

  6. Set the Key file name as StandardFunctionsKey and uncheck Protect my key file with password. Click OK to create the file:

    Getting ready

    Figure 12.10

  7. Save the project and build it. You should see the assembly created in the bin folder of the project:

    Getting ready

    Figure 12.11

  8. Now the assembly is ready. Let's see how it can be used with our reports.

How to do it...

The first thing we need to do is, make sure that the assembly can be accessed during development. This needs the assembly to be placed in the Reporting Services bin folder and add a reference adding it with Report Properties.

Follow these steps for accessing the assembly:

  1. Take a copy of StandardFunctions.dll and place it in bin folder of Reporting Services. The default path is C:\Program Files\Microsoft SQL Server\MSRS13.MSSQLSERVER\Reporting Services\ReportServer\bin.
  2. Open Chapter12 solution and get the Properties of the DailySales report. Note that you need make sure the DailySales report is modified as per the first recipe.
  3. Go to References page and click on Add to add a reference (see Figure 12.13). Browse to the placed StandardFunction.dll and select it. Click OK to add it:

    How to do it...

    Figure 12.12

  4. Once added, you should see the assembly in the grid. Click OK to save it:

    How to do it...

    Figure 12.13

Let's use the function in the assembly with the text box we have to display the selected date:

  1. Go to the Text Box Properties of the text box and open Font page. Click on expression icon to set the color through an expression:

    How to do it...

    Figure 12.14

  2. Set the following expression. Note the way of referencing the method. It is AssemblyName.ClassName.MethodName. If the namespace is different, then you need to mention it as well:
          =Iif(format(Code.GetDate(Parameters!ReportParameterSalesDate
          .Value), "yyyy-MM-dd") = "1900-01-01" 
          , StandardFunctions.Functions.GetTextColor("Error"),
          StandardFunctions.Functions.GetTextColor("")) 
    
  3. Click on OK to save the expression and click on OK again to save the text box properties:

    Note

    Remember, even though we could use the assembly during development, Reporting Services requires the assembly in the Global Assembly Cache (GAC) when running the report. Let's add the assembly to GAC.

  4. Open Command Prompt with administrative privileges (as an Administrator) and change the path to following folder (your path may be different based on the version of Visual Studio installed):

    C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools

    This folder has gacutil.exe and can be used for registering the assembly to GAC. Execute the following code to register it with GAC:

          gacutil.exe /i "<your folder that contains the
          dll>\StandardFunctions.dll"
    
  5. You should see a similar output with a success message:

    How to do it...

    Figure 12.15

Everything required is done. Let's publish the report and see whether it works:

  1. Go back to the project and deploy the report.
  2. Open the report using Web Portal. If you input an invalid date, the message set with text box should be displayed in red color:

    How to do it...

    Figure 12.16

How it works...

Remember, if the Reporting Services is installed in a different server, all you need to do is, register the assembly in GAC. If it is not registered, Reporting Services cannot access the assembly as it uses the assembly in the GAC when running the report.

The reference to the assembly is recorded in the .rdl file. If you open the .rdl, you will see a node that holds details of the assembly. In addition to this node, you will see an additional node if you have added Class Name and Instance Name with References page in Report Properties. This is not required for the code, we have written the function as a static method:

<CodeModules> 
    <CodeModule>StandardFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5021460a45a99ff0</CodeModule> 
</CodeModules> 
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime