Showing posts with label SSRS. Show all posts
Showing posts with label SSRS. Show all posts

Saturday, August 15, 2015

Embed SSRS Report Into WPF Application Without ReportViewer

When you google how to embed SSRS report into WPF application, chances are you'd come across this page at least once:

Walkthrough: Using ReportViewer in a WPF Application

But what if you don't want to use any of Windows Form controls? ReportViewer is Windows Form control.

There is WPF WebBrowser control we potentially could use, but it doesn't have a ready built-in attribute that we could simply set an URL to, so there's an extra step needed before we can use it. An attached property will do this for us.

    public static class WebBrowserHelper
    {
        public static readonly DependencyProperty WebAddressProperty = DependencyProperty.RegisterAttached(
            "WebAddress", 
            typeof (string), 
            typeof (WebBrowserHelper), 
            new PropertyMetadata(OnWebAddressChanged));

        public static string GetWebAddress(DependencyObject dependencyObject)
        {
            return (string) dependencyObject.GetValue(WebAddressProperty);
        }

        public static void SetWebAddress(DependencyObject dependencyObject, string value)
        {
            dependencyObject.SetValue(WebAddressProperty, value);
        }

        private static void OnWebAddressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WebBrowser browser = d as WebBrowser;
            if (browser != null && e.NewValue != null)
            {
                string url = e.NewValue.ToString();
                browser.Navigate(url);
            }
        }
    }

To use it, simply call the attached property from WebBrowser and bind it to the SSRS URL in the ViewModel.


<UserControl x:Class="Views.WebBrowserView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Views.Helpers"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">   
    <Grid>
        <WebBrowser local:WebBrowserHelper.WebAddress="{Binding Path=SsrsUrl}" />
    </Grid>
</UserControl>

public class ViewModel
{
    public string SsrsUrl { get; set; }
}

And we're pretty much done.

Friday, June 12, 2015

SSRS Multi Value Parameter And Stored Procedure

I'm always struggling when it comes to dealing with SSRS multi value parameter. There're basically two ways of doing this:
1) Use SRRS Filter
2) Use good old SQL

I definitely prefer the first approach because it's so much simpler. All we need is a Dataset which returns result set for all possible values of the multi-value parameter and then we filter it based on the selected items. Let's say we have a Dataset called Companies with field Name. Steps to filter:

  1. Create a multi value parameter, let's call it @Company and suppose it contains the following items: AAA, BBB, CCC, which are the distinct names in field Name of the Dataset Companies.
  2. Click on Dataset Properties and select Filters in the left-hand pane
  3. In the Expression drop-down list, select Name as the field to filter
  4. In the Operator drop-down list box, select the In operator
  5. In the Value box, type in [@Company]
And you're done!

Now the second approach will involve creating a Stored Procedure that takes in one string argument, which contains the values of the selected multi-value parameter all joined together with a delimiter that we will need to parse in our Stored Procedure to be able to use it in the SQL IN clause. Using the same example as before, this string argument is something like: "AAA,BBB,CCC". In the SSRS report, on the Parameters of the query definition, we will need to set the parameter value to something like:

    =Join(Parameters!Company.Value,",")
This method is definitely more complicated. We will need to write a SQL string split function in our Stored Procedure taking into account the possibility that the delimiter is not always going to be a comma and whether we need to handle entry with white space in between.