Reporting with Web Parts
I
n Chapter 8, we produced reports and learned how to deliver a report with a Windows serv-
ice. Now, let me introduce you to one more report delivery platform—SharePoint. Excited?
SharePoint is gaining popularity as an information delivery vehicle across the board to differ-
ent business users.
So, how can we take advantage of SharePoint to host our reports? Well, it is not as compli-
cated as you might be thinking. All you have to do is to develop reports using web parts and
later host the web parts with SharePoint.
I’d like to mention here that hosting web parts in a SharePoint environment is a topic far
beyond the reach of this book; I’ll only show you how you can develop a report with web parts
using Visual Studio 2005. You can go through Microsoft’s help files to learn more about Share-
Point technologies.
We’ll start by building your knowledge of web parts applications. After that, we’ll work on
a real-world practical reporting project.
This chapter will cover
• “Web Parts 101,” a step-by-step tutorial
• A brief examination of the web parts’ framework
• Producing a report with the ReportViewer hosted inside a web part
Web Parts 101
In Chapter 5, you learned to host reports with ASP.NET web applications. Would you be sur-
prised if I told you that we’ll use the same approach in this chapter too? However, there is a
difference, and that difference is the use of web parts. So, what are web parts? Well, in simple
words, web parts empower users to customize the site content according to their preferences.
Users can decide what information to display on a page and where that information looks best
on the page. Many of the most popular web sites are beginning to provide such customization;
sites like My MSN and My Yahoo are examples. Web parts were newly introduced with
ASP.NET 2.0 in Visual Studio 2005.
Creating a web parts application is the same as creating ASP.NET Web Forms. You’ll need
to start with creating an ASP.NET web project; however, you’ll notice that we’ll use Report-
Viewer inside a web part.
309
CHAPTER 9
8547ch09final.qxd 8/30/07 3:46 PM Page 309
Creating a Web Site Project
Open Visual Studio, and use the following steps to create a web site project; Figure 9-1 illus-
trates these steps:
1. Click File
➤
New
➤
Web Site.
2. In the Templates pane of the New Web Site dialog box, select ASP.NET Web Site.
3. From the Language drop-down, select Visual C#.
4. Name this web site; I’ve called it RSWebParts101. You may choose a different location
for storing the application files if you prefer. Please see Figure 9-1 for graphical presen-
tation of the naming.
5. Click the OK button to finish the process. After you click the OK button, VS will create a
new ASP.NET web site. Figure 9-2 shows the code produced and the files inside Solu-
tion Explorer.
Figure 9-1. Create a new ASP.NET web site.
As you will notice in Figure 9-2, the web site project contains the App_Data folder and
default.aspx page
. As y
ou kno
w
, a w
eb site can have many pages. However, I’ll keep it simple
and make use of the
default.aspx page for this project. Refer to Chapter 5 for the detailed
walk-thr
ough of creating an ASP.NET web site project.
CHAPTER 9
■
REPORTING WITH WEB PARTS310
8547ch09final.qxd 8/30/07 3:46 PM Page 310
Figure 9-2. The newly created web site
Adding Web Parts to the default.aspx Page
Before we start to add web parts on the page, let’s take a brief look at the web part framework.
The framework consists of the following items:
• The WebPartManager control
• Zones
• Editors
• Catalogs
The job of the WebPartManager control is to enable page customization. Zones define the
sections of the page that a user can customize. In this book, I’ll only talk about the web part
framework features needed to host our reports. Please check the following MSDN link for
more information on the web part framework:
/>Please use the following steps to customize default.aspx; Figure 9-3 illustrates adding
web part functionality to the page:
1. S
witch to design mode
.
2. D
r
ag
W
ebParts
➤
W
ebPar
tManager from the toolbox, and drop it onto the design
surface.
3. Drag WebParts
➤
WebPartZone from the toolbox, and drop it onto the design surface.
W
ebP
artManager plays a dual role here. First, it provides the ability to customize the page.
S
econd, it suppor
ts the personalization of the ASP.NET application. Personalization infor
ma
-
tion is stored in an SQL Server database, which is produced when the application is built for
the first time
.
CHAPTER 9
■
REPORTING WITH WEB PARTS 311
8547ch09final.qxd 8/30/07 3:46 PM Page 311
■
Note
Please make sure to add the WebPartManager control on the page before adding any of the web
parts.
Figure 9-3. Adding WebPartManager and WebPartZone to the project
Add the ReportViewer Control to the WebPartZone
Y
ou might be
wondering, no
w that we have the web part functionality all set up on the page,
how the web part will show the report. Well, all we have to do is to get help from the Report-
Viewer control. Let’s add ReportViewer using drag and drop; from the toolbox, drag Data
➤
R
epor
tView
er and drop it under the WebPartZone title (circled in Figure 9-4).
You may notice that after you drop the ReportViewer control, the WebPartZone size will
change to accommodate the ReportViewer. You may need to adjust the size to show maximum
r
epor
t view. Please make sure your page looks similar to Figure 9-4.
CHAPTER 9
■
REPORTING WITH WEB PARTS312
8547ch09final.qxd 8/30/07 3:46 PM Page 312
Figure 9-4. Adding the ReportViewer to the WebPartZone
Please make sure you set the properties outlined in Table 9-1.
Table 9-1. Property Settings for the Web Site
Object Property Value
WebPartZone1
HeaderText
Web Part 101 Report
Width
800px
Reportviewer1
Width
800px
You can also add the WebPartManager control in the page source view by adding the fol-
lowing tag after
<open>:
<asp:webpartmanager id="WebPartManager1" runat="server" />
After changing the properties, your page should look similar to the one shown in
Figure 9-5.
CHAPTER 9
■
REPORTING WITH WEB PARTS 313
8547ch09final.qxd 8/30/07 3:46 PM Page 313
Figure 9-5. Page layout with the web part in place
If you look at the source for this page, you’ll notice all the tags that we dropped onto the
page.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
➥
Inherits="_Default" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0,
➥
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
➥
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
➥
" /><html xmlns=" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:WebPartManager ID="WebPartManager1" runat="server">
</asp:WebPartManager>
</div>
<asp:WebPartZone ID="WebPartZone1" runat="server"
➥
HeaderText="Web Part 101 Report"
Width="800px">
<ZoneTemplate>
CHAPTER 9
■
REPORTING WITH WEB PARTS314
8547ch09final.qxd 8/30/07 3:46 PM Page 314
<rsweb:ReportViewer ID="ReportViewer1" runat="server"
➥
Height="271px" Width="800px">
</rsweb:ReportViewer>
</ZoneTemplate>
</asp:WebPartZone>
</form>
</body>
</html>
Building the Project
The build part is always exciting, right? After all, this is the time we can see our hard work in
action. Building a web site with web parts is similar to building a simple ASP.NET site. How-
ever, there are some minor considerations needed, which I’ll discuss as we build the project.
Now, let’s build it. As you know, you can build a project by clicking the small, green play
button in the main toolbox or pressing F5 on the keyboard to start the application in run-time
mode. If you’re building the project for the first time in debug mode, click on the OK button in
the “debug not enabled” information dialog box. This action will put debug-related settings in
the
Web.config file.
As I mentioned earlier, your first build will create an SQL Server personalization data-
base. So, depending on your computer’s speed, you’ll notice a delay in loading the site into
the browser. If all goes well, your project should compile without any issues, and you should
be able to see the web site in your default browser. Your browser output should be similar to
Figure 9-6.
Figure 9-6. Web site output in a browser window
Hooray! Congratulate yourself for successfully creating a web site and hosting Report-
V
iewer using a web part. I know the output is just a blank web part, but you’ll see a chart
report in action in the reporting project that’s coming up next.
CHAPTER 9
■
REPORTING WITH WEB PARTS 315
8547ch09final.qxd 8/30/07 3:46 PM Page 315
Before we move on to our reporting project, though, let me show you what happened to
t
he SQL Server personalization database,
A
SPNETDB
.
This database is store inside the
A
pp_Data
folder after the first build. Just refresh the App_Data folder inside Solution Explorer to see the
ASPNETDB.mdf file (shown in Figure 9-7). The personalization database is for the internal use of
the web site, to store information related to web parts.
Figure 9-7. Solution Explorer with the personalization database
Creating the Branch Sales Performance Chart
You’re working for Home Decorations, Incorporated as a developer. You have the task of
developing a series of reports primarily for use by upper management, and one of the
reports should produce a pie chart with the current fiscal year’s sales information. This
report will help management to compare across branches; it should meet all the charac-
teristics described in Table 9-2, and the report output should match Figure 9-8.
Table 9-2. Report Characteristics
Characteristics Value
Report Title Branch Sales Performance Chart (Detail section—Center aligned)
Company Title
Home Decorations Inc. (Detail section—Right aligned)
Logo
Yes (Detail section—Left aligned)
Print Date
Yes (Detail section—Right aligned)
Data Source
Manually populate DataSet
Columns to Report Branch, YearEndSalesTotal
Page Size Letter
Page Orientation Portrait
Lay
out Design No Header and Footer, all information should be in Body section
CHAPTER 9
■
REPORTING WITH WEB PARTS316
8547ch09final.qxd 8/30/07 3:46 PM Page 316