SSRS Extender Cache Class

This page is a Draft. Its content is not complete and might contain errors.

This page is part of the Class Library Pages collection.
Click the icon to see the index.

Overview

The SsrsExtender.Cache class is used within a SQL Server Reporting Services report to place field values in a Page Header or Page Footer section. An alternate method of accomplishing this, without using an external .NET assembly, can be found here.

Walkthrough

Source Code

SSRS Code-Behind

The following code can be added to each report in the Report Properties dialog > Code tab.

{copytext|SsrsSource}
Private Shared _dict As System.Collections.Generic.Dictionary(Of String, String)
'--------------------------------------------------------------------------------------------------
Public Shared Function CacheSet(ByVal name As String, ByVal value As String) As Boolean

    Dim result As Boolean = False
    Try
        name = name.ToUpper()
        If value IsNot Nothing Then
            CreateDictionary()
            If _dict.ContainsKey(name) Then
                _dict.Item(name) = value
                result = True
            Else
                _dict.Add(name, value)
                result = True
            End If
        End If
    Catch ex As Exception
        ' ignore exceptions
    End Try

    Return result

End Function
'--------------------------------------------------------------------------------------------------
Public Shared Function CacheGet(ByVal name As String) As String

    Dim result As String = ""

    Try
        name = name.ToUpper()
        CreateDictionary()
        If _dict.ContainsKey(name) Then
            result = _dict.Item(name)
        End If
    Catch ex As Exception
        ' ignore exceptions
    End Try

    Return result

End Function
'--------------------------------------------------------------------------------------------------
Private Shared Sub CreateDictionary()
    If _dict Is Nothing Then
        _dict = New System.Collections.Generic.Dictionary(Of String, String)
    End If
End Sub

VB.NET

{copytext|VbSource}
Imports System.Data.SqlClient
Imports System.Security.Permissions

Public Class Cache

    Private Shared _dict As Dictionary(Of String, String)
    '==============================================================================================
    Shared Sub New()

        Dim p As SqlClientPermission = New SqlClientPermission(PermissionState.Unrestricted)
        p.Assert()
        _dict = New Dictionary(Of String, String)

    End Sub
    '==============================================================================================
    Public Shared Function [Set](ByVal name As String, ByVal value As String) As Boolean

        Dim p As SqlClientPermission = New SqlClientPermission(PermissionState.Unrestricted)
        p.Assert()
        Dim result As Boolean = False
        name = name.ToUpper()
        Try
            If value IsNot Nothing Then
                If _dict.ContainsKey(name) Then
                    _dict.Item(name) = value
                    result = True
                Else
                    _dict.Add(name, value)
                End If
            End If
        Catch ex As Exception
            'ignore exceptions
        End Try

        Return result

    End Function
    '==============================================================================================
    Public Shared Function [Get](ByVal name As String) As String

        Dim p As SqlClientPermission = New SqlClientPermission(PermissionState.Unrestricted)
        p.Assert()
        Dim result As String = ""
        name = name.ToUpper()
        Try

            If _dict.ContainsKey(name) Then
                result = _dict.Item(name)
            End If

        Catch ex As Exception
            'ignore exceptions
        End Try

        Return result

    End Function

End Class

C#

{copytext|CsSource}
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Permissions;
using System.Data.SqlClient;

namespace MyNamespace
{
    public static class Cache
    {
        private static Dictionary<string, string> _dict;

        //-----------------------------------------------------------------------------------------
        static Cache()
        {
            new SqlClientPermission(PermissionState.Unrestricted).Assert();
            _dict = new Dictionary<string, string>();
        }
        //-----------------------------------------------------------------------------------------
        public static bool Set(string name, string value)
        {
            new SqlClientPermission(PermissionState.Unrestricted).Assert();
            bool result = false;
            name = name.ToUpper();
            try
            {
                if (value != null)
                {
                    if (_dict.ContainsKey(name))
                    {
                        _dict[name] = value;
                        result = true;
                    }
                    else
                    {
                        _dict.Add(name, value);
                        result = true;
                    }
                }
            }
            catch {/* ignore exceptions */}

            return result;
        }
        //-----------------------------------------------------------------------------------------
        public static string Get(string name)
        {
            new SqlClientPermission(PermissionState.Unrestricted).Assert();
            name = name.ToUpper();
            string result = "";
            try
            {
                if (_dict.ContainsKey(name))
                    result = _dict[name];
            }
            catch {/* ignore exceptions */}

            return result;
        }
    }
}