Be the first to write a review
Free - Dynamic Data Grouping using MS Reporting Services
Image: 1.0
Introduction
We hear this all the time, “Two birds with one stone.” What if I say, “Four birds with one stone”? I am sure four sound much better then two. So, what are my four birds and one stone?
My four birds are four distinct different outputs generated using source NorthWind->Orders (SQL Server 2000) and my stone is single physical Ms Reporting Services .rdlc file, which I am using as template to produce different outputs. This particular figure of speech is perfectly applicable to the technique, which I am going to share with you.
The application of this technique is not something new; we all have done same or similar while dealing with reporting of data. What is new here is the approach, which I can call to reuse of report (as we commonly reuse the code).
Let us discuss a practical scenario here. If I ask you, what kind of output you see in (image 1.0); you would probably say a simple report listing orders information. Well, you guessed it right. What will you do if the end-users want same report using data grouped by CustomerID(image 1.1)? In most cases, you might end up writing a new report. In this article, I will demonstrate how to reuse the report to produce the demanded output without the need of writing a new report.
I assume the reader of this article is comfortable using Visual Studio 2005, C#, Sql Server 2000 and Windows Forms. Basic understanding of how report designer works is helpful to work with attached code.
How can one report produce four different outputs?
Imagine you're asked to develop a Sales Order System; one of the reporting requirements involved is to produce four different reports to calculate freight paid for all shipped orders.
In a typical scenario, you will create four individual reports. Well, nothing wrong with this approach, we have done this in past. However, since we do lot of code reusing, why not try to reuse a report by creating a template that we can use to generate different outputs.
This brought Microsoft Reporting Service to my attention. I am having a fun time doing this report reuse business. I though, let me share this with my friends here with a hope that it helps you the way it helped me.
The key to make your report generate more then one output is to create a well designed template. Here is what I did to generate four different outputs from this one report.
Report designs considerations
Image: 1.4
A carefully designed template is required to create a multiple reports with different outputs. We will start by making use of Table Control; first, we have to identify and lay down all the details columns that are common to all outputs. Please check the repot in attached code for details of formatting etc.
Dynamic data grouping
The way information is grouped together will affect the output of the report (apart from the detail section). Now, you would start to wonder how I could change data grouping during runtime to create a different output.
Well, the solution to this problem is introducing some intelligence into our report, using a rendering engine that can leverage on and produce the desired output. I am making use of following two parameters to pass onto report so it can act differently.
Image: 1.5
I will make use of parReportType parameter to pass following four values: O-Orders, C-Customer, S-City and T-Country. This one letter type (O,C,S,T) provided as dynamic value to group the data before producing the output.
Image: 1.6
Our dose of intelligence to the report designer is nothing but following Grouping Expression, which changes data group based on information supplied through parReportType:
=iif(Parameters!parReportType.Value = "O","",
iif(Parameters!parReportType.Value = "C", Fields!CustomerID.Value,
iif(Parameters!parReportType.Value = "S",
Fields!ShipCity.Value,Fields!ShipCountry.Value)))
If you are not sure what iif() is, then not to worry, MS Reporting uses VB.NET syntax for coding expressions and custom code. If you have done any custom coding for example with Crystal Reports, then interacting with MS Reporting Services will not be a big deal.
The expression supplies instructions to the rendering engine on how to group and sort the data based on our choice of report selection. It starts with checking, if choice is "O" that means; create a simple output without grouping. Subsequently, check for rest of choices and switch the behavior of report generation.
We need to repeat the same expression in sorting tab of grouping and sorting properties window.
Asif Sayed
I started to code in 1990 using C and GW basic, then moved to 4GL world with DBIII, Clipper and FoxPro etc. and continued till I found and hooked to MS goody bag Visual Basic. For last four years, I am working extensively on .NET technologies and scripting both VB.NET and C#. My ideal choice of database is SQL Server; however, I do interact with Oracle when required. I have worked on all sort of Business applications, but my ideal project would be anything which has some Financial part associated with it, I wish I can put a Balance-sheet report in all the project I do… I also teach .NET related technologies as part-time faculty with local community college and try to help share my knowledge with others.