sql server reporting services 2005 for beginners

50
SQL Server Reporting Services 2005 for Beginners Wipro Technologies-TMTS Page 1 of 50 Nov 16 th , 2007 CCLksjdc SQL Server Reporting Services 2005 for Beginners Author Rekha S Adla Domain TMTS Date of Creation 16 th November ,2007 Wipro E-mail ID [email protected]

Upload: ajay-ch

Post on 29-Nov-2014

210 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 1 of 50 Nov 16th 2007

CCLksjdc

SQL Server Reporting Services 2005 for Beginners

Author Rekha S Adla

Domain TMTS

Date of Creation 16th

November 2007

Wipro E-mail ID Rekhaadlawiprocom

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 2 of 50 Nov 16th 2007

1 Introduction 3

2 Reporting Services Features 3

21 Authoring reports 3

22 Delivering reports 7

23 Report and Model Design 8

3 Parts of Reports 8

4 Defining a Report Layout 10

5 Report Development 10

51 Using the Report Wizard 11

52 Subreports 34

53 The Matrix 37

54 Adding drill downs 38

55 Adding dynamic sorting 39

55 Using Custom Code 40

6 Testing and Staging SSRS Applications 44

7 Publishing the reports 45

8 Deploying SSRS Solutions 47

81 Viewing reports 47

82 Setting report security 49

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 3 of 50 Nov 16th 2007

1 Introduction

With the SQL Server 2005 release Microsoft extends a major new component of its

integrated Business Intelligence platform SQL Server Reporting Services( expands

the Microsoft BI vision by making it easy to get the right information to the right

people ndash in any business environment

Reporting Services ships with SQL Server 2005 and includes

A complete set of tools for creating managing and viewing reports

An engine for hosting and processing reports

An extensible architecture and open interfaces for embedding reports or integrating

the solution in diverse IT environments

End user authoring capabilities for non-developers to build or modify their own

reports

2 Reporting Services Features

Reporting Services combines the benefits of a centrally-managed reporting system

with the flexibility and on-demand nature of desktop and web-based applications A

complete reporting platform Reporting Services supports the entire report lifecycle

from authoring through deployment

21 Authoring reports SQL Server Reporting Services provides a middle-tier server that runs under

Microsoft Internet Information Services (IIS)This allows us to build a reporting

environment on top of an existing Web server infrastructure We can build reports

that draw data from the data servers that we have in place for any data source type

that has a Microsoft NET Framework-managed data provider OLE DB provider or

ODBC data source

Reporting Services includes everything we need to start creating traditional or

interactive reports including a graphical report designer tool with report design

wizards

Report Authoring Feature Details

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 4 of 50 Nov 16th 2007

Report Authoring Feature Details

Wide range of supported data

sources

Microsoft SQL Server

Microsoft Analysis Services

Any OLE DB-compliant data source

Any ODBC-compliant data source

Flexible authoring tools Report Designer (uses Visual

Studio 2005)

XML-based Report Definition Language

(RDL)

Third party tools generating RDL

Flexible reporting formats Freeform

Table

Matrix

Charts

Parameterized reports using run-time

filtering

Sorting and grouping

Drill through

Linked reports

Flexible Query Editors SQL query generation tool

MDX query generation tool

(both in Report Designer)

Modular report execution Rendering is a separate process from

querying the same report may be

rendered in different formats

Execution can be scheduled or on-

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 5 of 50 Nov 16th 2007

Report Authoring Feature Details

demand

End User Authoring Report Builder for the end users to build

ad hoc reports

Report model designer (in BI

development studio)

Presentation formats Choose a presentation format when we open the report or

after we open the report We can choose Web-oriented page-oriented and desktop

application formats Formats include HTML MHTML PDF XML CSV TIFF and Excel

Custom controls or report items We can embed custom controls or report items

that we create or purchase from a third-party vendor A custom control requires a

custom report processing extension

Navigation We can add bookmarks and document maps to provide navigation

options within a large report

Aggregations We can aggregate and summarize data using controls and

expressions Aggregates include sum average min max count and running totals

Graphical elements We can embed or reference images and other resources that

contain external content

Dynamic Properties Support for adding custom code assemblies so that we can

provide dynamic functionality or special features in a report Run-time functionality

so that users can navigate a large report search for specific data export the report

to a file print the report and more

Managing reports

Reports can be rendered in both desktop and Web-oriented formats we can choose

from a variety of viewing formats to render reports on demand in preferred formats

for data manipulation or printing Reporting Services is server-based and thus

provides a way to centralize report storage and management provide secure access

to reports and folders control how reports are processed and distributed and

standardize how reports are used in the business

Reporting Services includes a web-based tool for managing reports the Report

Server Web Application Administrators can use this interface to define role-based

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 6 of 50 Nov 16th 2007

security for reports schedule report execution and delivery and track reporting

history Or an enterprise or ISV can use the Reporting Services Web Services APIs

to write customized management tools

Reporting Services can be configured for high availability We can install report

servers on single-server distributed and clustered configurations

Because the report definitions folders and resources are stored in a SQL Server

database we can use other tools such as SQL Server Management Studio to manage

metadata or use third party applications that take advantage of published APIs

Report Management Feature Details

Report metadata Name

Description

Data Source management Connections

Credentials

Parameter Management Defaults

Prompts

Report scheduling Integrated with SQL Server Agent

Execution properties Live cached or snapshot A Reporting

Services snapshot is a stored copy of the

report dataset ndash the results of the reportrsquos

source query when the report snapshot was

run

History of prior report executions Cataloged list of snapshots retained to be used

again as needed

Report security Users Groups and roles

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 7 of 50 Nov 16th 2007

Report Management Feature Details

Report Server Web Application Web-based management tool to

bull Define security

bull Schedule report execution and delivery

bull Track reporting history

Flexible management APIs Web Service API

22 Delivering reports We can post reports to a portal email them to users or allow users to use the web-

based report server to access reports from a folder hierarchy Navigation search

and subscription features help users locate and run the reports they need

Personalized subscriptions let them select the rendering format they prefer

Report Delivery Feature Details

Range of report rendering options bull Web formats (HTML)

bull Print formats (PDF TIFF)

bull Data (Excel XML CSV)

bull Others through open API

Flexible delivery options bull -driven

bull Scheduled

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 2: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 2 of 50 Nov 16th 2007

1 Introduction 3

2 Reporting Services Features 3

21 Authoring reports 3

22 Delivering reports 7

23 Report and Model Design 8

3 Parts of Reports 8

4 Defining a Report Layout 10

5 Report Development 10

51 Using the Report Wizard 11

52 Subreports 34

53 The Matrix 37

54 Adding drill downs 38

55 Adding dynamic sorting 39

55 Using Custom Code 40

6 Testing and Staging SSRS Applications 44

7 Publishing the reports 45

8 Deploying SSRS Solutions 47

81 Viewing reports 47

82 Setting report security 49

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 3 of 50 Nov 16th 2007

1 Introduction

With the SQL Server 2005 release Microsoft extends a major new component of its

integrated Business Intelligence platform SQL Server Reporting Services( expands

the Microsoft BI vision by making it easy to get the right information to the right

people ndash in any business environment

Reporting Services ships with SQL Server 2005 and includes

A complete set of tools for creating managing and viewing reports

An engine for hosting and processing reports

An extensible architecture and open interfaces for embedding reports or integrating

the solution in diverse IT environments

End user authoring capabilities for non-developers to build or modify their own

reports

2 Reporting Services Features

Reporting Services combines the benefits of a centrally-managed reporting system

with the flexibility and on-demand nature of desktop and web-based applications A

complete reporting platform Reporting Services supports the entire report lifecycle

from authoring through deployment

21 Authoring reports SQL Server Reporting Services provides a middle-tier server that runs under

Microsoft Internet Information Services (IIS)This allows us to build a reporting

environment on top of an existing Web server infrastructure We can build reports

that draw data from the data servers that we have in place for any data source type

that has a Microsoft NET Framework-managed data provider OLE DB provider or

ODBC data source

Reporting Services includes everything we need to start creating traditional or

interactive reports including a graphical report designer tool with report design

wizards

Report Authoring Feature Details

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 4 of 50 Nov 16th 2007

Report Authoring Feature Details

Wide range of supported data

sources

Microsoft SQL Server

Microsoft Analysis Services

Any OLE DB-compliant data source

Any ODBC-compliant data source

Flexible authoring tools Report Designer (uses Visual

Studio 2005)

XML-based Report Definition Language

(RDL)

Third party tools generating RDL

Flexible reporting formats Freeform

Table

Matrix

Charts

Parameterized reports using run-time

filtering

Sorting and grouping

Drill through

Linked reports

Flexible Query Editors SQL query generation tool

MDX query generation tool

(both in Report Designer)

Modular report execution Rendering is a separate process from

querying the same report may be

rendered in different formats

Execution can be scheduled or on-

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 5 of 50 Nov 16th 2007

Report Authoring Feature Details

demand

End User Authoring Report Builder for the end users to build

ad hoc reports

Report model designer (in BI

development studio)

Presentation formats Choose a presentation format when we open the report or

after we open the report We can choose Web-oriented page-oriented and desktop

application formats Formats include HTML MHTML PDF XML CSV TIFF and Excel

Custom controls or report items We can embed custom controls or report items

that we create or purchase from a third-party vendor A custom control requires a

custom report processing extension

Navigation We can add bookmarks and document maps to provide navigation

options within a large report

Aggregations We can aggregate and summarize data using controls and

expressions Aggregates include sum average min max count and running totals

Graphical elements We can embed or reference images and other resources that

contain external content

Dynamic Properties Support for adding custom code assemblies so that we can

provide dynamic functionality or special features in a report Run-time functionality

so that users can navigate a large report search for specific data export the report

to a file print the report and more

Managing reports

Reports can be rendered in both desktop and Web-oriented formats we can choose

from a variety of viewing formats to render reports on demand in preferred formats

for data manipulation or printing Reporting Services is server-based and thus

provides a way to centralize report storage and management provide secure access

to reports and folders control how reports are processed and distributed and

standardize how reports are used in the business

Reporting Services includes a web-based tool for managing reports the Report

Server Web Application Administrators can use this interface to define role-based

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 6 of 50 Nov 16th 2007

security for reports schedule report execution and delivery and track reporting

history Or an enterprise or ISV can use the Reporting Services Web Services APIs

to write customized management tools

Reporting Services can be configured for high availability We can install report

servers on single-server distributed and clustered configurations

Because the report definitions folders and resources are stored in a SQL Server

database we can use other tools such as SQL Server Management Studio to manage

metadata or use third party applications that take advantage of published APIs

Report Management Feature Details

Report metadata Name

Description

Data Source management Connections

Credentials

Parameter Management Defaults

Prompts

Report scheduling Integrated with SQL Server Agent

Execution properties Live cached or snapshot A Reporting

Services snapshot is a stored copy of the

report dataset ndash the results of the reportrsquos

source query when the report snapshot was

run

History of prior report executions Cataloged list of snapshots retained to be used

again as needed

Report security Users Groups and roles

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 7 of 50 Nov 16th 2007

Report Management Feature Details

Report Server Web Application Web-based management tool to

bull Define security

bull Schedule report execution and delivery

bull Track reporting history

Flexible management APIs Web Service API

22 Delivering reports We can post reports to a portal email them to users or allow users to use the web-

based report server to access reports from a folder hierarchy Navigation search

and subscription features help users locate and run the reports they need

Personalized subscriptions let them select the rendering format they prefer

Report Delivery Feature Details

Range of report rendering options bull Web formats (HTML)

bull Print formats (PDF TIFF)

bull Data (Excel XML CSV)

bull Others through open API

Flexible delivery options bull -driven

bull Scheduled

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 3: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 3 of 50 Nov 16th 2007

1 Introduction

With the SQL Server 2005 release Microsoft extends a major new component of its

integrated Business Intelligence platform SQL Server Reporting Services( expands

the Microsoft BI vision by making it easy to get the right information to the right

people ndash in any business environment

Reporting Services ships with SQL Server 2005 and includes

A complete set of tools for creating managing and viewing reports

An engine for hosting and processing reports

An extensible architecture and open interfaces for embedding reports or integrating

the solution in diverse IT environments

End user authoring capabilities for non-developers to build or modify their own

reports

2 Reporting Services Features

Reporting Services combines the benefits of a centrally-managed reporting system

with the flexibility and on-demand nature of desktop and web-based applications A

complete reporting platform Reporting Services supports the entire report lifecycle

from authoring through deployment

21 Authoring reports SQL Server Reporting Services provides a middle-tier server that runs under

Microsoft Internet Information Services (IIS)This allows us to build a reporting

environment on top of an existing Web server infrastructure We can build reports

that draw data from the data servers that we have in place for any data source type

that has a Microsoft NET Framework-managed data provider OLE DB provider or

ODBC data source

Reporting Services includes everything we need to start creating traditional or

interactive reports including a graphical report designer tool with report design

wizards

Report Authoring Feature Details

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 4 of 50 Nov 16th 2007

Report Authoring Feature Details

Wide range of supported data

sources

Microsoft SQL Server

Microsoft Analysis Services

Any OLE DB-compliant data source

Any ODBC-compliant data source

Flexible authoring tools Report Designer (uses Visual

Studio 2005)

XML-based Report Definition Language

(RDL)

Third party tools generating RDL

Flexible reporting formats Freeform

Table

Matrix

Charts

Parameterized reports using run-time

filtering

Sorting and grouping

Drill through

Linked reports

Flexible Query Editors SQL query generation tool

MDX query generation tool

(both in Report Designer)

Modular report execution Rendering is a separate process from

querying the same report may be

rendered in different formats

Execution can be scheduled or on-

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 5 of 50 Nov 16th 2007

Report Authoring Feature Details

demand

End User Authoring Report Builder for the end users to build

ad hoc reports

Report model designer (in BI

development studio)

Presentation formats Choose a presentation format when we open the report or

after we open the report We can choose Web-oriented page-oriented and desktop

application formats Formats include HTML MHTML PDF XML CSV TIFF and Excel

Custom controls or report items We can embed custom controls or report items

that we create or purchase from a third-party vendor A custom control requires a

custom report processing extension

Navigation We can add bookmarks and document maps to provide navigation

options within a large report

Aggregations We can aggregate and summarize data using controls and

expressions Aggregates include sum average min max count and running totals

Graphical elements We can embed or reference images and other resources that

contain external content

Dynamic Properties Support for adding custom code assemblies so that we can

provide dynamic functionality or special features in a report Run-time functionality

so that users can navigate a large report search for specific data export the report

to a file print the report and more

Managing reports

Reports can be rendered in both desktop and Web-oriented formats we can choose

from a variety of viewing formats to render reports on demand in preferred formats

for data manipulation or printing Reporting Services is server-based and thus

provides a way to centralize report storage and management provide secure access

to reports and folders control how reports are processed and distributed and

standardize how reports are used in the business

Reporting Services includes a web-based tool for managing reports the Report

Server Web Application Administrators can use this interface to define role-based

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 6 of 50 Nov 16th 2007

security for reports schedule report execution and delivery and track reporting

history Or an enterprise or ISV can use the Reporting Services Web Services APIs

to write customized management tools

Reporting Services can be configured for high availability We can install report

servers on single-server distributed and clustered configurations

Because the report definitions folders and resources are stored in a SQL Server

database we can use other tools such as SQL Server Management Studio to manage

metadata or use third party applications that take advantage of published APIs

Report Management Feature Details

Report metadata Name

Description

Data Source management Connections

Credentials

Parameter Management Defaults

Prompts

Report scheduling Integrated with SQL Server Agent

Execution properties Live cached or snapshot A Reporting

Services snapshot is a stored copy of the

report dataset ndash the results of the reportrsquos

source query when the report snapshot was

run

History of prior report executions Cataloged list of snapshots retained to be used

again as needed

Report security Users Groups and roles

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 7 of 50 Nov 16th 2007

Report Management Feature Details

Report Server Web Application Web-based management tool to

bull Define security

bull Schedule report execution and delivery

bull Track reporting history

Flexible management APIs Web Service API

22 Delivering reports We can post reports to a portal email them to users or allow users to use the web-

based report server to access reports from a folder hierarchy Navigation search

and subscription features help users locate and run the reports they need

Personalized subscriptions let them select the rendering format they prefer

Report Delivery Feature Details

Range of report rendering options bull Web formats (HTML)

bull Print formats (PDF TIFF)

bull Data (Excel XML CSV)

bull Others through open API

Flexible delivery options bull -driven

bull Scheduled

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 4: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 4 of 50 Nov 16th 2007

Report Authoring Feature Details

Wide range of supported data

sources

Microsoft SQL Server

Microsoft Analysis Services

Any OLE DB-compliant data source

Any ODBC-compliant data source

Flexible authoring tools Report Designer (uses Visual

Studio 2005)

XML-based Report Definition Language

(RDL)

Third party tools generating RDL

Flexible reporting formats Freeform

Table

Matrix

Charts

Parameterized reports using run-time

filtering

Sorting and grouping

Drill through

Linked reports

Flexible Query Editors SQL query generation tool

MDX query generation tool

(both in Report Designer)

Modular report execution Rendering is a separate process from

querying the same report may be

rendered in different formats

Execution can be scheduled or on-

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 5 of 50 Nov 16th 2007

Report Authoring Feature Details

demand

End User Authoring Report Builder for the end users to build

ad hoc reports

Report model designer (in BI

development studio)

Presentation formats Choose a presentation format when we open the report or

after we open the report We can choose Web-oriented page-oriented and desktop

application formats Formats include HTML MHTML PDF XML CSV TIFF and Excel

Custom controls or report items We can embed custom controls or report items

that we create or purchase from a third-party vendor A custom control requires a

custom report processing extension

Navigation We can add bookmarks and document maps to provide navigation

options within a large report

Aggregations We can aggregate and summarize data using controls and

expressions Aggregates include sum average min max count and running totals

Graphical elements We can embed or reference images and other resources that

contain external content

Dynamic Properties Support for adding custom code assemblies so that we can

provide dynamic functionality or special features in a report Run-time functionality

so that users can navigate a large report search for specific data export the report

to a file print the report and more

Managing reports

Reports can be rendered in both desktop and Web-oriented formats we can choose

from a variety of viewing formats to render reports on demand in preferred formats

for data manipulation or printing Reporting Services is server-based and thus

provides a way to centralize report storage and management provide secure access

to reports and folders control how reports are processed and distributed and

standardize how reports are used in the business

Reporting Services includes a web-based tool for managing reports the Report

Server Web Application Administrators can use this interface to define role-based

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 6 of 50 Nov 16th 2007

security for reports schedule report execution and delivery and track reporting

history Or an enterprise or ISV can use the Reporting Services Web Services APIs

to write customized management tools

Reporting Services can be configured for high availability We can install report

servers on single-server distributed and clustered configurations

Because the report definitions folders and resources are stored in a SQL Server

database we can use other tools such as SQL Server Management Studio to manage

metadata or use third party applications that take advantage of published APIs

Report Management Feature Details

Report metadata Name

Description

Data Source management Connections

Credentials

Parameter Management Defaults

Prompts

Report scheduling Integrated with SQL Server Agent

Execution properties Live cached or snapshot A Reporting

Services snapshot is a stored copy of the

report dataset ndash the results of the reportrsquos

source query when the report snapshot was

run

History of prior report executions Cataloged list of snapshots retained to be used

again as needed

Report security Users Groups and roles

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 7 of 50 Nov 16th 2007

Report Management Feature Details

Report Server Web Application Web-based management tool to

bull Define security

bull Schedule report execution and delivery

bull Track reporting history

Flexible management APIs Web Service API

22 Delivering reports We can post reports to a portal email them to users or allow users to use the web-

based report server to access reports from a folder hierarchy Navigation search

and subscription features help users locate and run the reports they need

Personalized subscriptions let them select the rendering format they prefer

Report Delivery Feature Details

Range of report rendering options bull Web formats (HTML)

bull Print formats (PDF TIFF)

bull Data (Excel XML CSV)

bull Others through open API

Flexible delivery options bull -driven

bull Scheduled

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 5: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 5 of 50 Nov 16th 2007

Report Authoring Feature Details

demand

End User Authoring Report Builder for the end users to build

ad hoc reports

Report model designer (in BI

development studio)

Presentation formats Choose a presentation format when we open the report or

after we open the report We can choose Web-oriented page-oriented and desktop

application formats Formats include HTML MHTML PDF XML CSV TIFF and Excel

Custom controls or report items We can embed custom controls or report items

that we create or purchase from a third-party vendor A custom control requires a

custom report processing extension

Navigation We can add bookmarks and document maps to provide navigation

options within a large report

Aggregations We can aggregate and summarize data using controls and

expressions Aggregates include sum average min max count and running totals

Graphical elements We can embed or reference images and other resources that

contain external content

Dynamic Properties Support for adding custom code assemblies so that we can

provide dynamic functionality or special features in a report Run-time functionality

so that users can navigate a large report search for specific data export the report

to a file print the report and more

Managing reports

Reports can be rendered in both desktop and Web-oriented formats we can choose

from a variety of viewing formats to render reports on demand in preferred formats

for data manipulation or printing Reporting Services is server-based and thus

provides a way to centralize report storage and management provide secure access

to reports and folders control how reports are processed and distributed and

standardize how reports are used in the business

Reporting Services includes a web-based tool for managing reports the Report

Server Web Application Administrators can use this interface to define role-based

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 6 of 50 Nov 16th 2007

security for reports schedule report execution and delivery and track reporting

history Or an enterprise or ISV can use the Reporting Services Web Services APIs

to write customized management tools

Reporting Services can be configured for high availability We can install report

servers on single-server distributed and clustered configurations

Because the report definitions folders and resources are stored in a SQL Server

database we can use other tools such as SQL Server Management Studio to manage

metadata or use third party applications that take advantage of published APIs

Report Management Feature Details

Report metadata Name

Description

Data Source management Connections

Credentials

Parameter Management Defaults

Prompts

Report scheduling Integrated with SQL Server Agent

Execution properties Live cached or snapshot A Reporting

Services snapshot is a stored copy of the

report dataset ndash the results of the reportrsquos

source query when the report snapshot was

run

History of prior report executions Cataloged list of snapshots retained to be used

again as needed

Report security Users Groups and roles

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 7 of 50 Nov 16th 2007

Report Management Feature Details

Report Server Web Application Web-based management tool to

bull Define security

bull Schedule report execution and delivery

bull Track reporting history

Flexible management APIs Web Service API

22 Delivering reports We can post reports to a portal email them to users or allow users to use the web-

based report server to access reports from a folder hierarchy Navigation search

and subscription features help users locate and run the reports they need

Personalized subscriptions let them select the rendering format they prefer

Report Delivery Feature Details

Range of report rendering options bull Web formats (HTML)

bull Print formats (PDF TIFF)

bull Data (Excel XML CSV)

bull Others through open API

Flexible delivery options bull -driven

bull Scheduled

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 6: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 6 of 50 Nov 16th 2007

security for reports schedule report execution and delivery and track reporting

history Or an enterprise or ISV can use the Reporting Services Web Services APIs

to write customized management tools

Reporting Services can be configured for high availability We can install report

servers on single-server distributed and clustered configurations

Because the report definitions folders and resources are stored in a SQL Server

database we can use other tools such as SQL Server Management Studio to manage

metadata or use third party applications that take advantage of published APIs

Report Management Feature Details

Report metadata Name

Description

Data Source management Connections

Credentials

Parameter Management Defaults

Prompts

Report scheduling Integrated with SQL Server Agent

Execution properties Live cached or snapshot A Reporting

Services snapshot is a stored copy of the

report dataset ndash the results of the reportrsquos

source query when the report snapshot was

run

History of prior report executions Cataloged list of snapshots retained to be used

again as needed

Report security Users Groups and roles

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 7 of 50 Nov 16th 2007

Report Management Feature Details

Report Server Web Application Web-based management tool to

bull Define security

bull Schedule report execution and delivery

bull Track reporting history

Flexible management APIs Web Service API

22 Delivering reports We can post reports to a portal email them to users or allow users to use the web-

based report server to access reports from a folder hierarchy Navigation search

and subscription features help users locate and run the reports they need

Personalized subscriptions let them select the rendering format they prefer

Report Delivery Feature Details

Range of report rendering options bull Web formats (HTML)

bull Print formats (PDF TIFF)

bull Data (Excel XML CSV)

bull Others through open API

Flexible delivery options bull -driven

bull Scheduled

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 7: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 7 of 50 Nov 16th 2007

Report Management Feature Details

Report Server Web Application Web-based management tool to

bull Define security

bull Schedule report execution and delivery

bull Track reporting history

Flexible management APIs Web Service API

22 Delivering reports We can post reports to a portal email them to users or allow users to use the web-

based report server to access reports from a folder hierarchy Navigation search

and subscription features help users locate and run the reports they need

Personalized subscriptions let them select the rendering format they prefer

Report Delivery Feature Details

Range of report rendering options bull Web formats (HTML)

bull Print formats (PDF TIFF)

bull Data (Excel XML CSV)

bull Others through open API

Flexible delivery options bull -driven

bull Scheduled

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 8: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 8 of 50 Nov 16th 2007

Report Delivery Feature Details

bullEvent Personalized subscriptions

bullRendered report or link delivery

bullData-driven subscriptions

bullOther applications through integration

SharePoint Integration Report Browser web part

Report Viewer web part

23 Report and Model Design

Report Designer Create reports in a full-featured report authoring application that

is hosted within Business Intelligence Development Studio We can use Report

Designer to work with data define a layout preview a report and publish a report to

a test or production server Report Designer includes query builders an expression

editor and wizards so that we can follow step-by-step instructions to create a report

Report Designer also supports advanced reporting features for professional report

designers who understand query languages and expression languages

Model Designer Create models that support ad hoc reporting in Reporting

Services We can generate models automatically based on an existing schema or

view refine the model and then publish it to a report server

Report Builder Create ad hoc reports using templates and pre-organized data and

save them directly to a report server Reports that we create in Report Builder can

be accessed distributed and managed just as we would any other report

3 Parts of Reports

The report Toolbox provides the building blocks for designing a client report

definition (rdlc) file in a visual environment The following items can be used in a

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 9: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 9 of 50 Nov 16th 2007

As with other Visual Studio 2005 projects we add items to a report by dragging tools

from the toolbox onto the Report Designer when the Layout tab is selected The

Reporting Services toolbox is shown in Figure 7

Figure 7 Reporting Services toolbox

To design a Reporting Services report drag the elements we want to use onto the

Report Designer

Text box is used to display single instance data Text boxes can be placed anywhere

on a report and can contain labels fields or calculated data We use expressions to

define data in text boxes

Table is a data region that we use to create tabular reports or to add table

structures to a report

Matrix is a data region that arranges data into columns and rows that intersect at

specific data points Matrices provide functionality similar to crosstabs and pivot

tables Unlike a table which has a static set of columns matrix columns can be

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 10: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 10 of 50 Nov 16th 2007

dynamic We can define matrices that contain static and dynamic rows and columns

Chart is a data region that we use to create visual data We can create a variety of

chart types

Image is used to display binary image data in a report We can use external

embedded or database images in bmp jpeg gif and png formats

Sub report is used to embed one report within another It can be a full report that

runs on its own or it can be a report that looks best when embedded within the

main report When we define a subreport we can also define parameters to filter

data in the subreport

List is a data region that we use to display repeating rows data for a single field or

to contain other report items

Rectangle is used as a graphical element or as a container for other report items

When we place report items within a rectangle we can move them with the

rectangle

Line is a graphical element that we can place anywhere on a page It has no data

associated with it

All items in a report including data groups table and matrix columns and rows

report items and the report itself have associated properties These properties

govern the appearance and behavior of the item

4 Defining a Report Layout

A report layout consists of three main areas a page header page footer and the

body The body of the report contains the report data We can add data regions text

boxes images rectangles subreports and visual enhancements to the report layout

Data regions display repeated rows of data from a data source The page header and

footer repeat the same content at the top and bottom of each page of the report We

can place report items such as images text boxes and lines in headers and footers

5 Report Development

The components installed from the SQL Server Express Toolkit that work with Visual

Studio 2005 are known as the Business Intelligence Development Studio (BIDS)

Report development is done in BIDS Reporting Services is installed after SQL Server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 11: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 11 of 50 Nov 16th 2007

Express Advanced Services is downloaded while BIDS is installed after the SQL

Server Express Toolkit is downloaded

Creating new SSRS reports through wizard

To launch BIDS and start a new Reporting Services project

To launch BIDS open the Start menu and select Run Programs Microsoft SQL

Server 2005 SQL Server Business Intelligence Development Studio

To start a new Reporting Services project select File New and then Project This

displays the screen in Figure 4

Figure 4 Creating a new project in Business Intelligence Development Studio

Select the Report Server Project project type

In SSE we have two choices for creating Reporting Services projects whereas in

other editions of SQL Server 2005 we have additional project types The choices in

SSE are

Report Server Project Wizard ndash Guides us through creating a basic report

Report Server Project ndash Creates a Reporting Services project but gives we more

control over the projects

51 Using the Report Wizard

There are two ways to create SSRS Reports We can build the report manually or

can use the Report Wizard For this first report we are going to take advantage of

the wizard Start a new Business Intelligence project in Visual Studio 2005 Then

from the menu select Project gt Add New Item Select the Report Wizard option and

call the file FirstReportWZrdl Click Add Click Next on the Welcome screen and

this will brings to the Select the Data Source screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 12: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 12 of 50 Nov 16th 2007

Select the New data source radio button and give the data source a name Select

Microsoft SQL Server as the type

Note

Use of the Shared data source option is actually a better option in most cases but

for the sake of this example we will just create a new data source Were going to

set up a shared data source later in this article

Click the Edit button to bring up the Connection Properties dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 13: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 13 of 50 Nov 16th 2007

Enter or select a server name The default is to log on using Windows

Authentication If we are using SQL Server Authentication choose that setting

and enter the username and password Finally select a database in this case

ReportingDemo Make sure of test connection

Next up is the Design the Query screen Query Builder can be by clicking the button

at the top left but in this case simply enter Select From Customer into the

Query string text box and click Next On the Select the Report Type simply select

the type (tabular or matrix) and hit Next

This brings up the Design the Table screen

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 14: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 14 of 50 Nov 16th 2007

Select State in the Available fields box and click the Group button This will group

the data in the result set by State The rest of the coloumns can be set as details of

the state group by clicking the details button

There is also a button to display the results in different pages for example each

state details in each page

Select Next On the Choose the Table Layout page select the Stepped option

and check the Enable Drilldown checkbox On the next screen choose a style for

the report (I chose Ocean) and click Next

This brings up the final screen Completing the Wizard

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 15: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 15 of 50 Nov 16th 2007

It shows a summary of the report options and gives we an opportunity to rename

the report and to preview it Click Finish to end the wizard

We should be shown the report in the standard Report Designer

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 16: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 16 of 50 Nov 16th 2007

There are three tabs Data Layout and Preview We should be on the Layout tab

Click on the Preview tab to view the report Since this report doesnt take any

arguments it will run as soon as we click on the tab

Use the +- symbols next to the State abbreviations to expand and collapse the

grouping of the report

52 Manual report creation

Using Report Wizard while perhaps overly simplistic does show we how to get a

report up and running with zero code and zero property setting Obviously such

reports will not meet most development requirements but do not discount this

option as a starting point for more complicated reports Once weve created a report

with the Report Wizard we are free to make any modifications to it

In this section we are going to create a more realistic report from scratch including

how to create and use a shared data source object stored procedures and how to

format the report set report properties and use report parameters

Creating a shared data source

Before we start the report we are going to build a shared data source ie a data

source that is common to and can be used by all of the reports on a reporting

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 17: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 17 of 50 Nov 16th 2007

server While each report can contain its own connection information it is good

practice to use shared data sources as it will save we a lot time and headaches This

way we only have to set the connection information once For example lets say the

company has Development Test and Production environments If we put the

connection information into each report we will have to change it each time when

we publish to Development Test and Production If we use a shared data source we

still have to set up a data source object for each of three environments but we can

simply publish the report to each environment and they will automatically use the

connection information associated with that environment

From the menu select Project gt Add New Item gt Select Data Source On the

General tab call the data source ReportsDB Leave the Type as Microsoft SQL

Server Click the Edit button on the right and enter the connection information for

Reporting Demo as before Click OK on the Shared Data Source screen and the

data source is done

Add a new report

From the menu select Project gt Add New Item Select Report and name it

FirstReportManrdl A new report will be added to the project and the Report

Designer will open at the Data tab At this point lets take a closer look at the

Report Designer tool At the top of the Report Designer window are three tabs

Data Layout and Preview The Data tab is used to build data sources for the

report The Layout tab is the physical report designer where we set up the header

the footer and the data presentation of the report The Preview tab allows we to

actually run the report from Visual Studio 2005 without having to publish it to a

report server first If the report takes parameters the Preview tab will ask we to fill

them out before it runs the report

The first thing we need to do is get data into our report At the top of the Data tab

choose ltNew Datasethellipgt from the dropdown list

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 18: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 18 of 50 Nov 16th 2007

The Dataset dialog opens

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 19: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 19 of 50 Nov 16th 2007

Name the dataset ReportData and select the ReportsDB data source from the

dropdown Select StoredProcedure as the command type and enter

spr_CustomerSelectAll in the query string box Click OK to close the dialog

Note

spr_CustomerSelectAll is a very simple parameter-less stored procedure that selects

all of the columns from the Customer table The source code is included in the

ReportingDemoDatabaseScriptsql script with the code download

Test the dataset by clicking on the big red exclamation point at the top of the report

designer This executes the query and displays the results We can repeat these

steps if we wish to add multiple datasets to the report

Setting up the report display

Next switch to the Layout tab This is where we actually build the report display

Start by dragging a table from the Toolbox onto the Body section of the report By

default the table shows a header row at the top a detail row in the middle and a

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 20: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 20 of 50 Nov 16th 2007

footer row at the bottom We can add additional columns to the table by right-

clicking on one of the columns and selecting one of the two Insert Column options

Click on the Datasets tab under Toolbox on the left-hand side of the Visual Studio

environment We should see the ReportData dataset Expand it select FirstName

and drag it to the first cell of the body row of the table This will display the First

Name field in the first column Now put the LastName in the second column and

the CustomerStatus in the third

Note that when we drag the column FirstName into the body row of the table

SSRS make a guess as to what to call the row It calls it First Name We can click

on the Preview tab to view the report

Adding formatting

Now we have a basic very plain report Lets add some formatting to make it look

good Lets add the DateOfBirth column to the table We can expand and shrink the

size of the columns by highlighting and dragging

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 21: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 21 of 50 Nov 16th 2007

If we wish to get rid of the footer row of the table and then click on any field in the

table We will see a grey box surround the table Right-click on the icon at the

beginning of the body row and then click the Table Footer option to deselect it

The table header is next To format all the cells in the header in the same way click

and drag over all the cells to select them Open the Properties window and set

background color font and so on

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 22: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 22 of 50 Nov 16th 2007

Note however that if we wish to change the justification of the text or the size then

we do so using the main menu at the top not via the properties window Again we

can click on the Preview tab to view the report

Add a report header

The table already has a header but the report can also have a header and footer

Report headers and footers appear on every page of the report Put the Report

Designer in Layout mode and select Report gt Page Header from the menu A

new band labeled Page Header appears on the report above the body Drag a text

box onto the header and click directly on it Type My First Report not in the

Properties window but right on the control

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 23: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 23 of 50 Nov 16th 2007

To adjust the text style and size of the heading highlight the text box and use the

option from the top menu

Formatting

Each field can have formatting Lets deal with that ugly Date of Birth field The

time information is superfluous so lets format the date properly Right-click on the

cell that contains the date of birth and choose Properties Next select the Format

tab

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 24: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 24 of 50 Nov 16th 2007

Click on the second button (labeled hellip) to the right of the Format code field

When the dialog opens use the standard options Select Date in the list on the left

then choose the sample date format we want on the right Click OK to close the

dialog

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 25: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 25 of 50 Nov 16th 2007

Click OK to close the Properties window and then click on the Preview tab to view

the report

Setting report properties

To access the report properties we need to be on the Layout tab of the Report

Designer When we click on this tab a new menu item Report activates This

menu gives we options for turning on and off the report header and footer sections

(as we have seen) adding embedded images setting report properties and setting

report parameters

Print Settings Setting up the report print settings is not intuitive compared to other products In

the report Layout tab we see the report with a ruler across the top That ruler is set

up in inches A standard sheet of paper is 85 x 11 inches The default setting in

SSRS is to have a 1-inch margin all the way around the content area So at 85

inches wide with a 1-inch margin on the left and another on the right we have 65

inches for content Any more and the content beyond the 65 inches will spill over

onto a second sheet of paper when printed It will not automatically shift to

Landscape

To get Landscape reports we have to make the proper report settings Navigate to

the Layout tab of the Report Designer From the Report menu select Report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 26: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 26 of 50 Nov 16th 2007

Properties Switch to the Layout tab of the dialog box To switch to Landscape

printing we need to change Page width to 11in and the Page height to 85in

On the same Layout tab we can also get up the report to display the data in

multiple columns and we can control the margins By default all the margins are set

at 1 inch Remember if the report width plus the right and left margins is greater

than the width of the printer we will get spill-over when we print out the report

Using parameters

There are two ways to add parameters to the reports The first is to use parameters

in the queries in the data sources The second is to set up parameters through the

Report Parameters dialog box We will do both

Query parameters

First we will set up parameters using a data source query Navigate to the Data tab

and click the hellip button right next the Dataset dropdown box The Dataset dialog

box opens and allows we to edit the query for the dataset Change the Query string

from spr_CustomerSelectAll to spr_CustomerSelectByState

Note

spr_CustomerSelectByState accepts a parameter called StateCD that allows us to

filter the data from the Customer table by State Again the source code is included

in the ReportingDemoDatabaseScriptsql script with the code download

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 27: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 27 of 50 Nov 16th 2007

Click OK and then run the query by clicking the red exclamation point () on the

Data tab toolbar We will be prompted to enter a parameter value for StateCD (the

State code) Enter CA and click OK The query should return all the customers that

live in California

Switch to the Preview tab Instead of the report just running there should be a

place for we to enter a value for the StateCD and run the report Enter CA and

click the View Report button We should see the report filtered by state

To edit the parameters or to add new ones switch to the Layout tab of the Report

Designer and open the Report Parameters dialog from the Report menu First

we are going to make the State code prompt a little more friendly Select StateCD

in the Parameters list box then in the Parameters Properties box change the prompt

to read State In the Available values section enter California and CA on the

first line Enter Louisiana and LA on the second line

This will make the prompt for State value into a dropdown We can also build

additional datasets into the report and use the values in those datasets as criteria

for dropdowns

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 28: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 28 of 50 Nov 16th 2007

Report parameters

Now lets add a parameter that is not used in a query Click the Add button in the

Report Parameters dialog from the Report menu Give the new parameter the

name ReportTitle Its Data type should be String Set the Prompt to Report

Title Click OK and close the dialog box

Drag a new text box onto the report header area Right-click on it and choose the

Expression option In the Edit Expression dialog select Parameters Double-click

on the ReportTitle parameter This text box will now display the value we pass to

the Report Title parameter

Switch to the Preview tab and try it out

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 29: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 29 of 50 Nov 16th 2007

Expressions

An expression is a short statement written in code which sets the value of a

property An expression can set the value of a property based on just about any data

available in the report parameters report global values fields in a dataset and

built-in functions

SSRS 2005 has made some drastic improvements in how expressions are created It

now supplies an intuitive expression editor If we scroll through the Properties

window we will notice that most of the properties of controls on the report can be

hard-coded or can be set through expressions (Thats the ltexpressiongt option we

see everywhere) We will also see buttons labeled fx in the pop-up Properties dialog

These buttons will access the expression editor

The expression feature makes SSRS 2005 into an incredibly versatile tool because it

allows most aspects of the report from the data it shows to how it shows it to

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 30: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 30 of 50 Nov 16th 2007

behave dynamically Rather than using a hard-coded value for a particular property

the SSRS resolves the expression to set the value for the property

Looking at our report lets say we want it to highlight all the customers from a

particular State We could use the expression

=iif(FieldsStateValue = CA Bold Normal)

If we apply this expression to the detail row in the table and run the report it will

bold the text for each row where the value of the State field is California Thats

great if the data from California is all that we are interested in However it would be

ridiculous to have one report for each state so lets make this expression even more

dynamic by adding a report parameter that will determine which State is highlighted

when the report is run

First lets add the new parameter to the reportNavigate to the Layout tab From

the Report menu select Report Parameters When the dialog window opens click

the Add button at the bottom of the screen Call the new parameter HighlightState

and set the available values as follows

California CA

Florida FL

Louisiana LA

Illinois IL

Texas TX

Now click on OK

Note

Well see that when setting up a list of pre-defined values for a parameter that there

is a From Query option We can use this option by defining a dataset on the data

tab of the report designer For example if we had a State table in the database we

could define a dataset to retrieve the data for the parameter dropdown from that

table Once we choose the From Query option we select the dataset the value field

(this is the field from the dataset that contains the value passed to the report) and

the label field (the field from the dataset that contains the value displayed to the

user when they are prompted for parameters)

The next step is to set up the expression Click on the table control on the report

When the table control is selected an extra control tab appears at the top and left of

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 31: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 31 of 50 Nov 16th 2007

the table Click on the little row maker icon just to the left of the detail row This

allows we to set properties for the entire row In the properties window select

FontWeight and choose ltexpressiongt from the drop-down list to open the

expression editor Typing =iif( should give we Intellisense We can use the

expression builder to create the following line of code

=iif(FieldsStateValue = ParametersHighlightStateValue Bold Normal)

The iif is a flow control statement commonly used in SSRS expressions Its format

is

iif (boolean statement true result false result)

If the Boolean statement is true then the true result is returned and if not the false

result is returned In this case the Boolean statement is asking Does the state code

equal the state code that was passed to the report If it does return the word bold

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 32: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 32 of 50 Nov 16th 2007

If it does not return the word normal The value returned is then used to set the

property for that instance of the row

Now switch to the Preview tab and select a State to highlight When the report

runs all the rows with customers from the selected State should be displayed in

bold

Common functions

Lets navigate back to the expression editor In the bottom half of the screen there

are three columns The first column lists different categories for building expressions

The last option is Common Functions Expand the Common Functions node Click

on the Date amp Time category In the next column double-click on one of the

functions We will see that it is added to the expression box at the top of the dialog

box Next simply put the cursor in the expression box after the function If we then

type in the opening left bracket we should instantly see Intellisense for the function

we selected

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 33: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 33 of 50 Nov 16th 2007

Lets use the Year function in the report to display only the year in which someone

was born instead of their full birth date Right-click on the field that displays the

date of birth and select Properties On the General tab find the Value field and

click the fx button to bring up the expression editor Change expression to read

=Year(FieldsDateOfBirthValue)

There we have it Its a good idea to preview the report now

Also of note here are the aggregate functions These functions assist we in working

with all of the data in a dataset For example lets say the report is an order form

with a list of items and their prices We can use the Sum function on the Price field

to create a total price on the report In a different example if we had a report that

listed all the purchase dates and the total amount of each purchase we could use

the AVG function to get the average purchase price for that customer

Global variables

Lets go back again to the expression editor This time click on Globals in the first

column of the expression editor In the second column we will see the list of the

global variables that are provided to the report

In this example we are going to use the global variables to create a page-numbering

scheme for our report First add a footer to the FirstReportManrdl report We

enable this by going to the Layout tab and selecting the Report Footer option from

the Report main menu Now that we have a footer drag four text boxes into the

footer section Set their font properties to 8 pt bold Set the text for each text box

as follows

Page

=GlobalsPageNumber

of

=GlobalsTotalPages

Switch to the Preview tab and run the report We will now have a footer on each

page that looks something like Page 1 of 5

Some other globals worth noting are

Execution Time ndash this tells we how long it took to run the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 34: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 34 of 50 Nov 16th 2007

ReportServerUrl ndash this allows we to verify that we are getting the report from the

correct source

UserID - this shows under which User ID the report was run

We can use these global variables to build some basic diagnostics into the reporting

We could also add these variables to the report footer and use a parameter to hide

or show them That way even in production the users can see a tidy clean report

but we can see the same report with all the extra information on it

52 Subreports

A subreport is a report that is embedded into another report Subreports can take

parameters and execute their own datasets A key aspect to note is that a subreport

in SSRS is actually just another report (unlike some reporting tools where a

subreport is a special construct) In fact in SSRS we can execute a subreport on its

own

To add a subreport to a report we simply drag a subreport control onto the report

and tell it which report to display If the subreport requires parameters we have to

tell the main report which value to pass to the subreport Its actually very simple

Lets add a new report to the project and call it MainReportrdl Create a new

dataset using the shared datasource and the query

SELECT CustomerID FirstName LastName FROM Customer

Switch to the Layout tab Drag a table on the report detail area Set up the first

column to display the customers first name (by dragging that column from the

Datasets tab into the Detail row) and set up the second column to display the

customers last name Label the third column Address Preview the report just to

be sure it works

Create another report and call this one MySubReportrdl This time create a

dataset that uses the shared data source and use the following query text

SELECT Address City State ZipCode

FROM Customer

WHERE (CustomerID = CustomerID)

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 35: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 35 of 50 Nov 16th 2007

In the Layout tab use text boxes to create the address layout as shown in figure 5

We can simply drag the text boxes onto the screen by clicking on the field in

datasets tab and dragging it onto design surface We will also note that when we do

this the expression used to set the value property for the textbox uses the First()

function This function will use the value from the first row returned by the dataset

All other rows returned are ignored

Now preview the report and use 100 for the CustomerID parameter

Jump back to the MainReportrdl To embed the subreport drag a SubReport

control into the detail cell for the column we labeled Address Right-click on the

SubReport control and select Properties In the Properties dialog choose

MySubReport from the subreport dropdown

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 36: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 36 of 50 Nov 16th 2007

Next switch to the Parameters tab This is where we connect the subreport to the

main report We do this by indicating which value from the main report is to be

passed to the subreport to fulfill its parameter requirements

In the Parameter Name column choose CustomerID and in the Parameter Value

column choose =FieldsCustomerIDValue This will wire up the subreport to

whichever customer is being displayed in the row of the table

Click OK to close the dialog and then preview the main report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 37: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 37 of 50 Nov 16th 2007

53 The Matrix

The Matrix is used to create pivot table style reports We are going to set up a simple

Matrix control to show us which customers in which State have which status

First start a new report called MatrixReportrdl Add our standard dataset where

we use the shared data source and then add the Select from Customer query

Navigate to the Layout tab and drag a new Matrix control onto the page Where it

says Rows drag the State field from the dataset Where it says Columns drag the

CustomerStatus field from the dataset Drag first name and last name into the data

area on the Matrix control After a little formatting it should resemble this

When the report runs it looks like this

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 38: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 38 of 50 Nov 16th 2007

54 Adding drill downs The drill down functionality in SSRS allows we to have areas of the report that can

expand and collapse much like a tree view

First start a new report called AdvancedTablerdl Add our standard dataset

where we use the shared data source and the Select from Customer query

Navigate to the Layout tab and drag a new table control onto the page The first

thing we need to do is to add a group to the table Click the table to highlight it then

right-click on the little box with three horizontal lines in it that appear at the left side

of the table at the beginning of detail row Select Insert Group When the Group

Dialog appears use =FieldsStateValue as the expression so that the report now

groups on State Now drag the State field into the first column on the Group row

Next drag the First Name Last Name and City fields into the detail row Add

some formatting and preview the report

Weve now performed a grouping but we still have to enable the drilldown Click on

the table to highlight it Right-click on the nub that appears just to the left of the

detail row Select Properties When the Properties window opens expand the

visibility section Set the Hidden property to True and set the ToggleItem property

to State The Hidden property determines the state of the row when the report is

first run If we set it to True then the data is collapsed and hidden By setting the

ToggleItem property to State when the report is run a little + sign appears next to

it when the report is run and it works like a tree view

Switch to the Preview tab and run the report Now only the States appear but we

can see the customer in each State by using the tree-style +- controls

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 39: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 39 of 50 Nov 16th 2007

55 Adding dynamic sorting It is incredibly easy to add sorting to the table Lets re-open the

FirstReportManrdl report Navigate to the Layout tab of the report designer Click

in the cell labeled First Name in the header of the table Right-click and select

Properties Switch to the Interactive Sort tab Check the checkbox for Add an

interactive sort action to this textbox Next set the Sort expression to the value

that is displayed in the column ndash in this case =FieldsFirstNameValue Now repeat

this process for the rest of the report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 40: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 40 of 50 Nov 16th 2007

Preview the report and we should have interactive sorting on each column

55 Using Custom Code SQL Reporting Services offers the software developer a variety of options when it

comes to customizing reports through the use of code These options give the

software developer the ability to write custom functions using NET code that can

interact with report fields parameters and filters in much the same was as any of

the functions that come built in This gives the developer the ability to extend the

capabilities of SRS far beyond those that are available out of the box

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 41: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 41 of 50 Nov 16th 2007

Following are covered in this section

Using code to customize the report

Custom code for use within the report using code embedded in the report

Custom code for use within the report using a custom assembly called by the report

There are two ways to add custom code to the reports embedded code and code

within custom assemblies This code can then be used in the report through

expressions Generally we will add custom code to the report when we need to

perform complex functions and we need the capabilities of a full programming

language to accomplish them

Embedded Code in the Report

Embedded code is by far the easiest method to implement for two main reasons

First we simply add the code directly into the report using the report designers UI

Second this code becomes a segment within the reports rdl file making its

deployment simple because it is a part of the report and will be deployed with it

Custom functions

There are about 100 common functions in SSRS 2005 and they can handle most of

what we will need to do Occasionally however we will still need to set an

expression with more control than the common functions can manage So to make

SSRS even more flexible we can write custom VBNET or C functions and use them

in expressions

Now we are going to add a custom function to the report

Open the report and navigate to the Layout tab From the Report menu select

Report Properties and then jump to the Code tab We are going to write a custom

function that returns a different color depending on the value that is passed to it

We will then use that function to set the background color for the status field in the

report detail table

Copy the following code into the code window

Public Function GetColor(ByVal status as String) as String

IF status = 100 Then

Return White

End IF

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 42: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 42 of 50 Nov 16th 2007

IF status = 101 Then

Return Yellow

End IF

IF status = 102 Then

Return Tomato

End IF

End Function

Click OK and close the window

Note

The full list of available colors is found in the Color dropdown list in the properties

window

Now that we have a function that returns color names we need to wire up that

function to an expression Click on the Customer Status cell and open the

Properties window Find the Background Color property and choose Expression

from the dropdown Add the following line of code to create an expression

=codeGetColor(FieldsCustomerStatusValue)

When the report runs and this expression is resolved SSRS will call the GetColor

function and pass the value of the customer status for that instance of the row to the

function The function takes over and decides which color should be returned SSRS

then uses that value as the value for the background property for that cell

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 43: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 43 of 50 Nov 16th 2007

Please note that custom functions must be called using =codeltmyfunctiongt

Now navigate to the Preview tab and run the report

Note The embedded code can contain multiple methods Embedded code must be

written in Visual Basic NET and must be instance based

Note Referencing many of the standard NET assemblies in the custom code

requires that we create a reference in the report To do this go to the References

Tab of the Report Properties click on the ellipses by the References Assembly name

grid and then select the appropriate assembly that we want to reference These will

only have Execution permission by default

The Code section of the report was primarily designed for basic use of the NET

framework and the VBNET language syntax itself Access to many framework

namespaces is not included by default in the Code element While it is possible to

use other NET Framework assemblies directly within the Code section of the report

it is highly recommended that we consider using a custom assembly

One of the primary reasons for this is Security By default the Code element runs

with Execute permission only so if we perform certain operations such as reading

data from a file we will need to modify the code group for the report expression host

assembly When we change the permissions for the code that runs in the Code

element it changes it for all reports that run on that reporting server

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 44: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 44 of 50 Nov 16th 2007

If we need to use features outside of the VBNET language syntax have more

complicated logic or need to use more of the NET Framework we should move the

code into a custom assembly We can then reference that assembly in the report and

call the assembly through a method in the custom class This way we can add the

permission set and code group for the custom code without having to modify the

permissions for all code that runs in the Code element

Another reason is that in SRS we do not have the benefit of developing the Code

section of the report using the full Visual Studio IDE with all of the features such as

IntelliSense and Debugging features at the disposal Writing code in the Code section

of the report is not much different than working in NotePad

Tip If the code we do choose to place in the Code element is more than just a few

simple lines of code it can be easier to create a separate project within the report

solution to write and test the code A quick VBNET WinForm or Console project can

provide the ideal way to write code we intend to embed in the report We get the full

features of the IDE and once we have the method or methods working the way we

want we can just paste them into the code Window of the report Remember to use a

VBNET project since the Code element only works with code written in VBNET

6 Testing and Staging SSRS Applications

Itrsquos easy to test and stage the SSRS applications Our approach to testing and

staging depends on the companyrsquos policy for testing and staging any application

Some companies simply test on a development workstation and publish right to a

server Others have more rigorous testing requirements which is recommended

The first step to testing is to preview the reports The Report Designer has a

Preview tab Click this tab to display the report Figure 8 shows an example in the

Business Intelligence Development Studio

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 45: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 45 of 50 Nov 16th 2007

Figure 8 Previewing a report in BIDS

Once we know that the report behaves the way we expect we are ready to move the

report onto a staging server or into production depending on how the organization

manages its testing and staging environments If we plan to deploy the SSRS

applications to a staging server follow the instructions in the next section

7 Publishing the reports

Up to now we have been running the reports in the Visual Studio 2005 environment

but to make them useful we must publish them to a report server The easiest way

to do this is to have Visual Studio publish the reports

Start by right-clicking on Project in the Project Explorer and choose Properties

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 46: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 46 of 50 Nov 16th 2007

Set TargetServerURL to the URL of the report server Use the

TargetReportFolder property to set up a folder for the reports If the folder does

not exist the Publisher will create it for we The other key thing to pay attention to

here is the OverwriteDataSources property When this property is set to True it

will automatically copy over all the data source objects when we publish When set to

False it will copy any new data source objects but it will not overwrite existing

ones This is important when dealing with development test and production servers

We can publish a set of data source objects to each server pointed at the correct

database and never have to worry about what database the reports are hitting in

each environment

From the Build menu select Deploy Solution This will publish the project to the

selected folder on the report server We can also deploy individual reports by right-

clicking on the file in the Solution explorer and selecting Deploy

Once the report has been published we can access and run it on the server through

the browser at httpltservernamegtltreportservernamegt From the Home

page we should be able to find the folder we published to with the reports in it

Select a report to run At the top of the page we can enter any values for report

parameters and then run the report From here we can also print or export the

report

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 47: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 47 of 50 Nov 16th 2007

8 Deploying SSRS Solutions

When we make the SSRS reports available on a staging or production server we do

so by deploying them The reports will need the data sources that supply data to the

reports We can choose whether to overwrite a data source of the same name on the

SSRS server during deployment The default behavior is to not overwrite existing

data sources but to overwrite existing reports

Before we deploy the reports we must configure the deployment properties for the

SSRS project Right-click the name of the project in the BIDS Solution Explorer to

display the screen shown in Figure 9

Figure 9 SSRS Reporting project properties

We must configure these properties before we can deploy the report

OverwriteDataSources ndash Enables we to overwrite the shared data sources that we

have configured in the project The default is False

TargetDataSourceFolder ndash Folder to create on the server where the shared data

sources will reside

TargetReportFolder ndash Folder to create on the server where the reports will reside

This lets we classify the reports however we wish

TargetServerURL ndash URL of the SSRS Web service This is probably the

ReportServer virtual directory on the target server

After we configure the properties for the deployment target we can deploy the

target Right-click the name of the project in BIDS Solution Explorer and select

Deploy The reports (and optionally data sources) will be deployed to the specified

target server

81 Viewing reports There are two ways to users can view reports

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 48: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 48 of 50 Nov 16th 2007

Report Manager ndash Users can view reports by using Report Manager a Web-based

application that is included with Reporting Services By default Report Manager is

available at httplocalhostreports$SQLEXPRESS

Important Note If we have named the database instance other than SQLEXPRESS

replace the text SQLEXPRESS in the above URL with the name of the instance

The Report Viewer control ndash This control enables we to embed reports in the

applications by using Visual Studio 2005 The Report Viewer control is available for

both Windows applications and Web applications

The Report Viewer control has two operating modes The first displays reports that

are hosted on a Reporting Services Web service The second displays embedded

client-side reports that are not hosted on the Web service similar to the way we

embed Crystal Reports into Visual Studio applications

The version of SSRS that is included with SSE makes reports available via the Report

Server Web service To use the Report Viewer control for developing client-side

reports we can use Visual Studio Web Developer Express or Visual Studio 2005

Standard Edition or higher

The example presented here covers the following operations

Use SQL Server 2005 Express with Advanced Services to create a report

Open Visual Studio 2005 and create a Windows application

Embed an instance of the Report Viewer control onto a form and bind it to the Web

service

Figure 10 shows a Visual Studio 2005 solution with a Windows project This project

contains a single blank form

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 49: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 49 of 50 Nov 16th 2007

Figure 10 Selecting the Report Viewer control

Drag the Report Viewer control onto the form Then set the properties of the report

to execute as shown in Figure 11 Remember that SSRS that comes with SSE can

publish reports that the Report Viewer control can use

Figure 11 Selecting Report Server properties

To select a specific report select ltServer Reportgt from the Choose Report menu

Specify the Report Server Url and the Report Path The Report Server Url is the

URL of the SSRS Web service This is typically httpltserver_namegtreportserver

The Report Path is the path to which we publish the reports Figure 11 shows a top-

level folder called DailyReports with a report name of Report1 The first character

of the report path must always be a slash

82 Setting report security Report security is configured by using role-based settings In role-based security we

use roles that are assigned sets of permissions We assign specific users or groups to

those roles Role-based security is very easy to implement

SSE comes with predefined roles We cannot change these roles nor can we create

new ones The following predefined roles are available to SSRS in SSE

Browser ndash Use to view folders reports and to subscribe to reports

Content Manager ndash Use to manage all aspects of content including creating

folders reports and data sources

My Reports ndash Use to publish reports create folders and manage resources in the

My Reports folder

Publisher ndash Use to publish reports

Report Builder ndash Use to view report definitions (RDL)

To assign a Windows user or group to a predefined role

In Internet Explorer open Report Manager by going to

httplocalhostreports$SQLEXPRESS

Ensure that we are viewing the Report Manager home page

Click the Properties tab to display the screen shown in Figure 12

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1
Page 50: SQL Server Reporting Services 2005 for Beginners

SQL Server Reporting Services 2005 for Beginners

Wipro Technologies-TMTS Page 50 of 50 Nov 16th 2007

Figure 12 Viewing users and roles

To assign a user to a role click the New Role Assignment button This displays the

screen shown in Figure 13

Figure 13 Adding users to a role

By default report security is inherited from the assignments that we set here To

change the security settings for an individual report navigate to the report click the

Security tab and making the desired changes However if we do this the report

will no longer inherit from its parent

  • sectionToggle0
  • sectionToggle1