|
Adding Parameters to a Report
|
|
In the article titled
Reporting Services - A First Look, we saw the basic architecture of SQL
Server Reporting Services and saw how a simple report can be authored with
ease. Having created a basic report, it is often a common need to have
parameters to a report so that a user can look at only the required
information. For example, if you are showing a list of authors to the user, it
would be useful to have a parameter that allows the user to select a state and
then see the list of authors belonging to that state. By not having this
parameter, a user would be forced to look at all the authors although his
interest would have been only on authors from a particular state. SQL Server
Reporting Services has excellent support for parameters. The following are the
features available:
In this article we will see how to use some of the above features. Note that we will not go into step-wise discussions on how to use the report designer. Most of the basics are covered in my earlier article Reporting Services - A First Look. I however will provide detailed information wherever required. Simple Parameters using Lists Let us first see how to define and use simple parameters that derive their values from a list. The list in our case, can be a table. As an example to demonstrate this, we will define a report that has a parameter called state. This parameter will show all the state values from the authors table. The user can then select a particular state and then view all the authors belonging to that state. The following are the steps that you will need to follow to define this report.
At this stage, if you finish the report wizard, Visual Studio will crank for a while and then create the basic report definition along with the appropriate parameters. Let us now see how the parameters have been defined.
![]() That was easy! Note that SQL Reporting Services
automatically adds a toolbar kind of interface to the top of the report where
the parameters are shown. Also, a button is added for you to click and view the
report. As you add more parameters, this piece of the interface will change
appropriately.
One small glitch that you will notice in this report is
that a user can view only authors from a particular state. What if you want to
view all the authors across all the states?? For this, we possibly will
want a value in the parameter list called All, which when
choosen will show all the authors. There is no out-of-the box support for such
a feature and you basically will need to modify your query a bit. Here is how
we can do it.
If you now switch to the Preview tab, you can see the parameter list having an All option. Choose this option and click on the View Report button to view all authors. The other options continue to perform as they are, which is to show authors belonging to a particular state. Using Dependent Parameters In the earlier example, we used a single parameter that fetched its values from a table. Typically, reports can have more than one parameter that are dependent on each other. For example, you can have a parameter that shows the list of departments and then on selection show a list of employees belonging to that department. SQL Reporting Services allows you create such dependent parameters with ease. In this example, we are going to use the AdeventureWorks database that is installed along with Reporting Services to demonstrate the dependent parameters concept. The AdventureWorks database is a vast improvement over the pubs database that was originally shipped with SQL Server. AdeventureWorks contains tables with company data pertaining to sales, human resources, manufacturing and production, and purchasing and is ideal for writing many kinds of reports. For this example, we will show the user a list of product categories and for each category, the user will then be able to select a sub-category. The report then, will display all the products belonging to the sub-category along with relevant information. This report will also include an image of the product as an illustration of how easy it is to integrate with image columns in the database. To create this report, follow these steps:
At this stage, we can test these the parameters in isloation to ensure that the required information is being populated. Choose Report > Report Parameters. The dialog already shows our productCategoryID parameter. We will just need to modify the prompt and data source information as shown in the following figure: Since we want all products belonging to a sub-category
(based on the data model of AdventureWorks), we will add one more parameter
while we are in this dialog. Click on the Add button and add a
new parameter called productSubCategoryID. Modify the prompt
and data source information as shown in the following figure.
![]() At this stage, we can test our report by clicking on the
Preview tab. You should now see two parameter combo boxes shown.
The first combo will list the various categories and if you select one of them,
the next combo-box will refresh to show the various sub-categories. We will now
create the dataset for the product list and then design the actual report.
Switch to the Data tab and create a new data set. The new data
set will be named Products and will use the following query.
Note that we use the new parameter that we created above
to ensure that all products for a sub-category are fetched. We also do an OUTER
JOIN with the photos table, since not all products have their images captured.
We will now design the actual report. Switch to the Layout tab
and then drag and drop the following fields to the layout canvas as shown.
Before dragging the fields, you will need to drop a table onto the canvas. The
table will help us to display a tabular report.
![]() Note that the Thumbnail field requires
special treatment. Instead of a text field, you will need to drop an image
control into the canvas and then choose Database as the
source. Then, point to the thumbNail field in the Products
data set. After this, you can add whatever embellishments you want to make the
report look nice. Once done, click on the Preview tab and then
choose a category and sub-category. For example, if you choose Bike
and Touring Bike, the following report will be shown:
![]() We have shown only a portion of the report above, but note
the ease with which you have generated this report! You can deploy the project
to the report server so that you can access these reports from
a browser.
That brings us to the end of this article. In this article
we saw the use of simple parameters and dependent parameters. There is more
that you can do with parameters, but we will defer that to a later article. The
concepts covered here are sufficient to get you exploring with the basic
feature set of parameters. Have fun!!
|
| Home |