Welcome!

Microsoft Cloud Authors: Janakiram MSV, Yeshim Deniz, David H Deans, Andreas Grabner, Stackify Blog

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

Customizing SQL Server Reporting Services with .NET Code

Design and customize reports with Visual Studio .NET integrated Report Designer

With the release of Reporting Services, Microsoft has provided a truly enterprise-level reporting solution. Reporting Services provides support for the entire reporting life cycle including report authoring, report management, and report delivery. This article introduces the process of designing and customizing reports using the Visual Studio .NET integrated Report Designer. Report Designer allows .NET programmers the ability to customize and extend reporting functionality. Custom fields added to a report can include calculations, text manipulation, and expressions. If a report needs to include complex programming logic, programmers can add functions to the report through code blocks embedded into the report definition. To take advantage of the full capabilities of the .NET Framework, reports have the ability to reference external .NET assemblies. By completing the hands-on exercises in this article, you will gain an understanding of how to enhance the functionality of your reports through expressions, code blocks, and external assemblies.

Reporting Services: An Overview
Microsoft Reporting Services is a robust, cost-effective, server-based reporting tool. It consists of a middle-tier server built on top of an SQL Server back end in conjunction with a Web-based front-end running under IIS. Although Report Services relies on SQL Server for its data repository, the report data can come from any data repository that has an OLEDB, ODBC, or .NET managed provider. Microsoft even supports the ability to extend the API to write custom providers. As the product matures, you can expect to see more managed providers released by the major database vendors in the market today.

Reports are designed through a Report Designer that, when installed, integrates with Visual Studio. Using the Report Designer, programmers leverage their knowledge of .NET programming in a familiar design-time environment. Using the Report Designer, you can easily create a variety of complex reports including tabular, matrix, drill-through, and parameter-driven reports. The Report Designer renders the report in a Report Design Language (RDL) format. RDL is an open XML-based schema containing standard tags used to define the data retrieval and report layout information. By creating and storing a report in RDL format, Report Services decouples the creation of the reports from the report rendering. For example, the same RDL document can be rendered as HTML in a Web page link and as a subscription-based PDF e-mail attachment. Out of the box, Report Services supports rendering in many popular formats, such as HTML, MHTML, PDF, and Excel. Developers can also extend the rendering API to include their own rendering formats.

Creating a Report
Once installed, the Report Designer integrates with Visual Studio. A new Business Intelligence Project folder shows up in the New Projects dialog (see Figure 1). There are two options to create a report project. Creating the project using the wizard launches a series of screens that guide you through the common steps necessary for creating a standard report. The other option is to create a blank report project and run through the steps manually.

The following activity demonstrates using the Report Wizard to create a standard report:

1.  Create a new project in Visual Studio. Select the Business Intelligence Project folder, name the project reportDemo1, select the Report Project Wizard template, and click OK.
2.  Click the Next button on the Report Wizard welcome screen. The next screen gathers information to set up a data source for the report (see Figure 2). Data sources can be private to a particular report or shared among several reports. Create a new data source called dspubs by clicking on the Edit button. Enter the appropriate information to create a link to the pubs database in the Data Link Properties window. After testing the connection, close the window and click the Next button on the Report Wizard screen.
3.  The next step is to specify the query to retrieve the data for the report. You can either type in the query string directly or click the Edit button. This launches a familiar query designer interface. Right-click the top portion of the designer to add tables. Add the authors, titleauthor, and the titles tables to the designer. Select the au_lname, au_fname, state, title, type, price, and ytd_sales columns from the tables. Right click on the top section of the designer and choose run to verify the query returns data. Click the OK button in the bottom right corner of the designer to close the window. Click the Next button on the Report Wizard screen.
4.  In the Design the Table window, select the state field for the page level, the type field for the group level, and the remaining fields for the details level (see Figure 3).
5.  Select a tabular report type and click the Next button.
6.  Click the Next button and choose a stepped layout.
7.  Click the Next button and choose a style.
8.  Click the Next button, name the report authorSales, and click the Finish button.

As you can see, creating a basic report using the wizard is quick and painless. After running through the wizard, the designer presents the report in a layout screen that you can use to further refine and customize the report.

Customizing Report Fields
If you are familiar with creating reports using Access or Crystal Reports, customizing fields in the Report Designer should be intuitive. The following activity demonstrates customizing fields using the Report Designer. If you do not already have the demo report project up and running, launch the project in Visual Studio.

1.  There are three tabs at the top of each report open in the report designer: Data, Layout, and Preview. Make sure you are on the Layout tab and right- click on the ytd_sales textbox and select Properties, which launches the Textbox Properties window (see Figure 4).
2.  Change the format to Currency and click the Advanced button in the lower left corner of the window.
3.  As you click through the tabs in the Advanced Textbox Properties window, you will notice that most properties allow you to customize the property using custom code. Click on the Font tab and select the Function button next to the size property. Enter the following Immediate If expression that changes the font based on the ytd_sales amount.

=IIF( Fields!ytd_sales.Value > 5000,"14pt"," 9pt")

4.  Close the Edit Expression and the Advanced Property windows. Click on the Preview tab at the top of the report. Notice that all ytd_sales above 5,000 are rendered in a 14-point font.
5.  Although the Immediate If statement is useful for Boolean test conditions, a Switch expression is useful for testing multiple conditions and works like the Select Case VB statement. To demonstrate the Switch expression click on the Price textbox in the report Layout tab. In the Properties window, click on the Color drop-down and select Expression.
6.  Enter the following Switch expression that changes the color property based on the price:

=Switch( Fields!price.Value > 20,"Blue", Fields!price.Value > 15, "Green", Fields!price.Value > 5, "Red")

7.  Close the Edit Expression window. Click on the Preview tab at the top of the report. You should notice the color of the price changing depending on the amount.

As you can see, adding conditional formatting to a report is easy. The cool part is that you can change almost any formatting property based on a runtime condition. You can also base your condition on any field exposed to the report. For example, see if you can change the color of an author's last name depending on what state he/she lives in.

Adding Functions to a Report
Although conditional formatting is very useful, it is somewhat limited in both scope and functionality. Suppose we want to create custom code for reuse throughout a report. You can include embedded code written in Visual Basic by adding methods to a globally defined code member class. This embedded code is saved in the report definition file and has report level scoping.

The following activity demonstrates creating and consuming embedded code in a report. If you do not already have the demo report project up and running, launch the project in Visual Studio.

1.  Select the Layout tab of the report in the Report Designer window. On the Report menu, choose New Report Properties, which launches the Report Properties dialog box.
2.  Select the Code tab and enter the method shown in Listing 1. Because the Code tab is just a text box, you may want to write the code in a class file and then paste it in the box. This will allow you to take advantage of the Visual Studio code editing features.
3.  Right-click on the state textbox in the Report Layout designer and select Expression to launch the Edit Expression dialog. Enter the following code in the Expression textbox:

=code.FilterEmptyStrings(Fields!state.Value)

4.  Repeat Step 3 for the au_fname textbox passing in the value of the au_fname field.
5.  Update the author table in the pubs database so that some of the records have blank state and au_fname fields.
6.  Preview the report. You should notice the report renders "no value entered" if the state or au_fname fields are blank.

Extending Reports Through External Assemblies
Although adding functions to a report is a great way to extend and customize your reporting solutions, it is still somewhat restrictive. Code reuse is limited to a single report and the full feature set of the .NET framework is not available. To extend code access across multiple reports and take full advantage of the .NET framework, you create an external .NET assembly that the report references.

The following activity demonstrates the process of referencing external assemblies from a report. If you do not already have the demo report project up and running, launch the project in Visual Studio:

1.  Add a Class Library project to the solution named reportHelperLib. Add a class named rptHelper.
2.  In the rptHelper class, add the methods shown in Listing 2. Notice that one is a shared method. This will help to demonstrate the difference between calling shared methods and instance methods from a report.
3.  Build the Class Library project. To create a reference to the reportHelperLib.dll from your report project, open the Report Properties dialog and select the References Tab (see Figure 5). In addition to the assembly, you need to include the class and create an instance name to reference instance methods of the class. You also need to place a copy of the reportHelperLib.dll in the bin folder of the Report Designer folder. This ensures that you can preview the report in the designer. The default location for this folder is: "C:\Program Files\Microsoft SQL Server\80\Tools\Report Designer"
4.  Select the Layout tab of the report in the Report Designer window. Right-click the price column in the table and insert a column to the right. Label the column tax. Select the textbox in the column row and change the name to tax.
5.  Right-click on the tax textbox and select Expression to launch the Edit Expression dialog. Enter the following code in the Expression textbox. Because this is a shared method, the fully qualified name references the assembly:

=reportHelperLib.rptHelper.getTax( Fields!state.Value)

6.  Preview the report. The tax column contains the value returned by the getTax method.
7.  Select the Layout tab of the report in the Report Designer window. Right click on the au_fname textbox in the Report Layout designer and select Expression to launch the Edit Expression dialog. Replace the existing code in the Expression textbox with the following code. Because this is an instance method, we reference it through the class instance variable created in Step 3.

=code.m_rptHelper.FilterEmptyStrings(Fields!au_fname.value)

8.  Preview the report. Notice the report renders "no value entered" if the au_fname field is blank.

Review
With the release of Reporting Services, Microsoft has provided a truly enterprise-level reporting solution. Reporting Services provides support for the entire reporting life cycle, including report authoring, report management, and report delivery. This article has introduced the process of designing and customizing reports using the Visual Studio .NET integrated Report Designer. A future article will explore the issues involved with report management and delivery. It will guide you through the process of publishing, securing, and executing reports.

Full Reporting Life Cycle Support
SQL Server Reporting Services supports the full reporting life cycle, including:

  • Report authoring: Report developers can create reports to be published to the Report Server using Microsoft or third-party design tools that use Report Definition Language (RDL), an XML-based industry standard used to define reports.
  • Report management: Report definitions, folders, and resources are published and managed as a Web service. Managed reports can be executed either on-demand or on a specified schedule, and are cached for consistency and performance.
  • Report delivery: SQL Server Reporting Services supports both on-demand (pull) and event-based (push) delivery of reports. Users can view reports in a Web-based format or in e-mail.

More Stories By Dan Clark

Dan is a Microsoft Certified Trainer, Microsoft Certified Solution Developer, and a Microsoft Certified Database Administrator. For the past seven years he has been developing applications and training others how to develop applications using Microsoft technologies. Dan has been developing and training Microsoft's .NET technologies since the early betas. He has recently authored the book "An Introduction to Object-Oriented Programming with Visual Basic .NET," published by Apress.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Jensays 12/03/08 11:00:20 AM EST

I'm so glad I found your advice, I was struggling with a conditional font size in a SRS report, whereby I kept getting a syntax error. Thankfully, your article clearly showed me what my error was.

Mike Kissinger 03/24/05 09:45:38 AM EST

Figure 5 is a blurr. Can you send me a clear image?

Mike

@ThingsExpo Stories
DevOps at Cloud Expo – being held October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real r...
While some developers care passionately about how data centers and clouds are architected, for most, it is only the end result that matters. To the majority of companies, technology exists to solve a business problem, and only delivers value when it is solving that problem. 2017 brings the mainstream adoption of containers for production workloads. In his session at 21st Cloud Expo, Ben McCormack, VP of Operations at Evernote, will discuss how data centers of the future will be managed, how th...
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that TMC has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo and Big Data at Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Global buyers rely on TMC’s content-driven marketplaces to make purchase decisions and navigate markets. Learn how we can help you reach your marketing goals.
SYS-CON Events announced today that Enroute Lab will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enroute Lab is an industrial design, research and development company of unmanned robotic vehicle system. For more information, please visit http://elab.co.jp/.
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
SYS-CON Events announced today that Daiya Industry will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Daiya Industry specializes in orthotic support systems and assistive devices with pneumatic artificial muscles in order to contribute to an extended healthy life expectancy. For more information, please visit https://www.daiyak...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
SYS-CON Events announced today that App2Cloud will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct. 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. App2Cloud is an online Platform, specializing in migrating legacy applications to any Cloud Providers (AWS, Azure, Google Cloud).
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are now increasingly adopting Agile practices across their organizations in order to successfully navigate the disruptive waters that threaten to drown them. In our quest for establishing change as a core competency in our organizations, this business-centric notion of Agile is an essential component of Agile Digital Transformation. In the years since the publication of the Agile Manifesto, the conn...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, will introduce two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a mu...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
Mobile device usage has increased exponentially during the past several years, as consumers rely on handhelds for everything from news and weather to banking and purchases. What can we expect in the next few years? The way in which we interact with our devices will fundamentally change, as businesses leverage Artificial Intelligence. We already see this taking shape as businesses leverage AI for cost savings and customer responsiveness. This trend will continue, as AI is used for more sophistica...