Again, select the project under Solution Explorer, right-click it, select Add ➤New Item, and then select Crystal Report from the Add New Item dialog box.. Figure 12-7.CR report selection
Trang 1Moving from Crystal Reports
to Reporting Services
I’m sure that you enjoyed the versatility of the client-side RS with various reporting projects
we’ve done so far Now, my question to you is, “What was report development like before the
client-side RS in VS 2005?” Well, I’m sure the majority of developers would say that they
worked with Crystal Reports (CR) I’m also included in this majority; I’ve used CR extensively
with various commercial projects so far
Although CR and RS are two different technologies, they produce the same result—a report My rationale behind including this chapter in this book is to help you move toward the
RS platform from CR If you haven’t used CR in the past, this chapter will help you to get to
know some facts about CR, and you’ll also see how report output similar to RS’s can be
pro-duced using another technology
This chapter will cover
• A side-by-side comparison of RS and CR
• Developing reports using both RS and CR Which tool is preferable for developing reports, RS or CR? Tough one, right? Both the tools come as out-of-the-box solutions with VS 2005 for reporting on a variety of data sources As
with any competitive tools, each contains features that aren’t in the other I’d like to remind
you, though, that our discussion is based on the client-side capabilities of both technologies
In my personal opinion, two choices are always better than one However, I find it much easier
to work with RS One of the highlights that most motivates me to use RS is the free hand given
to developers on the report’s design surface I can have a chart and a table at the same time
inside the body section; imagine that! RS has brought a whole new functionality for
develop-ers, who can design reports just like they design Windows or Web Forms—just pick the report
item, and place it where you want; it’s that simple
Before we continue our discussion further, if you are new to CR, you should know that CR
is a reporting tool produced by Business Objects that has been an integral part of the Visual
Studio series, including the 2005 release CR can gather data and design reports from a wide
range of data sources, such as SQL Server, Oracle, and spreadsheets like Microsoft Excel You
can get more information on CR here:
http://www.businessobjects.com/products/reporting/crystalreports/net/vsnet.asp
369
C H A P T E R 1 2
Trang 2You might wonder why Microsoft is still shipping CR with Visual Studio Well, since much development exists for CR, Microsoft would like to support it At the same time, by introduc-ing RS on the client side, Microsoft is empowerintroduc-ing developers to be more productive usintroduc-ing this new reporting tool By including both tools, Microsoft gives developers the ability to choose for themselves which platform suits them best As for long-term commitment, I’ve seen both CR and RS as report development tools shipped with the Visual Studio 2008 release And why a side-by-side reporting project? I think this way you can feel how easy it is to develop the report using RS Instead of just telling you that creating reports using RS is easy, let me show you with the help of this exercise
Comparing RS and CR Side by Side
The reporting project in this chapter differs from the reporting projects you’ve done so far Why? Because, you’ll see the same report output from two different tools side by side So, if you’re familiar with CR development, you can see how to leverage your existing skills and learn the RS equivalent If you’ve not yet developed any CR reports, this is your chance to see the CR in action
Let’s develop a simple, tabular Books Year to Date Sales report for a publisher This report will have a standard header and footer Year-to-date sales information will be part of the body section As Figure 12-1 shows, we’ll see two different report previews Although the report content will look the same, you’ll see how reporting is made easy in VS 2005 with these two out-of-the-box solutions
Figure 12-1.Side-by-side report preview
Trang 3Creating the Windows Forms Project
Please open Visual Studio, and use the following steps to create a Windows application project;
see Figure 12-2 for an illustration of these steps:
1. Click File ➤New ➤Project, or press Ctrl+Shift+N on the keyboard
2. In the “Project types” pane of the New Project dialog box, select Visual C# ➤Windows
3. In the Templates pane, select Windows Application
4. Let’s give a name to the application; I’ve called the project RSandCR You may choose
a different location for storing the application files according to your preference
5. Click the OK button to finish the process; VS will create a new Windows application project You’ll also notice that a new form with the name Form1 is part of the project
Figure 12-2.Create a new Windows application.
Let’s move on and add the dataset and the ReportViewer to the project So, as we have two different reports here, do we need the two datasets? Well, no, we can use the same dataset for
both the reporting engines Let’s start by selecting the project under Solution Explorer;
right-click it, and select Add ➤New Item ➤DataSet Let’s name the dataset dsRSandCR
Trang 4As you know, we use the ReportViewer to display the report output for RS Similarly, we need to add the CrystalReportViewer to display the report output of CR Add the ReportViewer
to the project by dragging Data ➤ReportViewer from the toolbox and dropping it onto the design surface Similarly, add the Crystal ReportViewer by dragging Crystal Reports ➤ Crystal-ReportViewer from the toolbox and dropping it onto the design surface Please see Figure 12-3 for the illustration of these steps
Figure 12-3.Project after adding the dataset and ReportViewers
Please make sure you set the properties in Table 12-1 After you specify all the properties, your Form1 should look similar to the Figure 12-4
Table 12-1.Property Settings for the Project
Object Property Value
crystalReportViewer1 Size 750, 240
Figure 12-4 shows the references used by the RS and CR ReportViewers These references are automatically added to the project when ReportViewers are placed in Form1
Trang 5Figure 12-4.Form1 with RS and CR ReportViewers
Step 1: Creating a Data Table
Since we already have the dataset in the project, it’s time to add a data table to it Please use
the following steps to add a data table inside the dataset:
1. You can go to the dataset designer in two ways: double-click dsAccess inside Solution Explorer, or right-click the dsRSandCR node and select View Designer
2. Right-click the design surface, and select Add ➤DataTable
3. Click the header of the newly created data table, and name it dtRSandCR Let’s start adding columns to dtRSandCR by right-clicking DataTable and selecting Add ➤Column
4. Add the following columns into the data table; your data table should look like the one shown in Figure 12-5:
• BookTitle (System.String)
• AuthorLastName (System.String)
• Price (System.String)
• YTDSales (System.Double)
Trang 6Figure 12-5.Final look of the dtRSandCRs data table
Step 2: Designing the Report Layout
Before we start designing the report, let’s take a moment to analyze the report layout in Figure 12-1 Although the output of both reports looks the same, we need to use different approaches to report design You’ll use the table report item for the RS part of this exercise How about CR? Do we have the table report item in the CR design tool set also? I know many questions like these might be going through your mind, but hold on to them for now
As we go through each step of the report development, you’ll see the different approaches to producing the same report unfold
All right, we’ve got our dataset in place with a data table and all the necessary columns We’re all set to start working on designing the report layout As you know, we need two reports: one each for RS and CR The process to add reports in CR is similar to one we use for RS; the only difference is to select the proper report type
Let’s start adding the RS report by selecting the project under Solution Explorer Right-click it, and select Add ➤New Item Next, select Report from the Add New Item dialog box (shown in Figure 12-6) Please name the report rptRS.rdlc Click the Add button to complete the process
Let’s add the CR report now Again, select the project under Solution Explorer, right-click
it, select Add ➤New Item, and then select Crystal Report from the Add New Item dialog box Please name the report rptCR.rdlc Click the Add button to complete the process
Figure 12-6.Adding the new RS and CR reports
Trang 7As soon you add the CR report to the project, you’ll notice that the Crystal Reports Gallery dialog box will greet you Please make sure to select the As a Blank Report radio button, and
click the OK button, as shown in Figure 12-7
Figure 12-7.CR report selection
Notice that the two new reports are part of the project now and that a new toolbox called Data Sources is available with our dataset information inside RS Similarly, a new toolbox
called Field Explorer is available with the information needed to design in CR Now, we’re
ready to move on to the design phase Figure 12-8 shows the initial default design layout for
RS and CR
Figure 12-8.Initial default report layout for RS and CR
Trang 8As you can see in Figure 12-8, both the RS and CR reports are open in design mode ini-tially You might be wondering why the CR version looks more complete, and the RS version is just a body section Well, the RS approach is free form in nature, so you can add the sections that you need In CR, even if you don’t need headers and footers, they are there inside the report by default So we need to add the header and footer only to the RS report
Another observation about CR is it has two sets of headers and footers Unlike RS, CR has
a report header and footer and a page header and footer Why? Well, if a developer needs a sec-tion to appear only one time at the beginning, or end, of a report, this secsec-tion should appear
in the report header, or report footer
Adding the Header and Footer
Since we don’t need to add the header and footer to the CR report, let’s continue with the RS report As usual adding the header and the footer is simple: all you need do is right-click the open area inside report designer and select Page Header; then right-click the open area again, and select Page Footer Since we’ll make use of only the page header and footer of the CR report, we can easily adjust the height of the report header and footer to zero by changing the properties or dragging the edges of the header and footer Please make sure both reports look similar to Figure 12-9
Figure 12-9.Report layout in RS and CR with headers and footers
Setting Up the Page
Let’s work on the page setup now As in previous RS projects, right-click the open area on the design surface, and select Properties; you may wish to put your name in the Author field and information about the report to the Description field I’d advise you to let all other choices stay
at their defaults
Now, the question is, “How can we do a similar page setup for CR?” Well, the steps are simple: right-click inside a blank area of the header, footer, or body of the design surface, and
Trang 9select Design ➤Page Setup or Printer Setup Page Setup is used to set page margins Page size
and orientation are set using the Printer Setup option Figure 12-10 shows how to access page
and printer setup options
Figure 12-10.CR report page and printer setup
Designing the Page Header and Footer
Now that we’ve added all the necessary sections to our report, let’s work on the header and
footer Drag one Textbox item, and drop it inside the header and add another one inside the
footer section As previously, report item properties are changed by selecting the report item,
right-clicking it, and selecting Properties or by accessing general properties toolbox
Selecting each text box, and specify the values for each report item’s properties according
to Table 12-2
Table 12-2.Report Item Properties for the Header and Footer
Report Item Property Value
textbox1
Name txtReportTitle Value Books Year To Date Sales Font Normal, Arial, 10pt, Bold textbox2
Name txtPageNumber Value ="Page " & Globals!PageNumber & " of " & Globals!TotalPages
Trang 10Now, it’s the CR report’s turn for header and footer design We’ll have the same report title and page number for the CR report This time, we need to use the text object (instead of the TextBox option) by selecting Crystal Reports ➤Text Object and dragging and dropping it inside the header section After you drop the object, you can type the report title inside the
text object; please make sure to type the report title as Books Year To Date Sales Similar to the
way we change the properties in the RS text box, please change the Font property of the text object to Normal, Arial, 10pt, Bold
The page number is treated as special field in CR As you know, in RS, we can create spe-cial field equivalents by using expressions Let’s add the page number to the CR report by right-clicking inside the page footer section, and selecting Insert ➤Special Field ➤Page N
of M; see Figure 12-11
Figure 12-11.The CR Text object and Page Number special field
Designing the Body Section
Both of our reports have the header and the footer properly set now, so let’s focus on the design of the body section For the RS report, we’ll make use of the table report item Let’s start
by dragging Report Items ➤Table from the toolbox and dropping it inside the body section in the report designer A new table item is part of the report now, and it’ll have a default name of table1 We need to add one more column, so right-click the rightmost column header and select “Insert Column to the Right”
As usual, you could choose to map the data table columns to the text boxes by either typing an expression or dragging and dropping from the data source Let’s drag Source ➤ dsRSandCR➤Book Title and drop it inside the first column of the table item detail section
Trang 11Repeat the task for the rest of the columns from dsRSandCR Let’s set the table row’s top and
bottom borders to solid lines, as we’ve done in the past Please make sure your report design
surface looks like Figure 12-12
Figure 12-12.The RS report after adding header, footer, and body sections
Let’s work on the details (body) section of CR report now As you know, with RS, we don’t have to do anything special to reference the typed dataset However, in CR, we need to make
reference to it Making reference is easy: right-click inside any section, and select Database ➤
Database Expert A new Database Expert dialog box will appear Expand Node ➤Project Date
➤ADO.NET DataSets, and move dsRSandCR to the Selected Tables section on the right side
Please see Figure 12-13 for an illustration of these steps
Figure 12-13.Making reference to the typed dataset with CR report