RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Getting Started with Crystal Reports for Visual Studio 2005 : Page 4

Learn how Crystal Reports and Visual Studio 2005 combine to help streamline one of the most common development tasks: creating reports in business applications.

Displaying Charts
Crystal Report also supports the creation of graphical charts. In this section, you will see how you can display a chart showing the percentages of a particular product ordered by the each customer.

Add a new Crystal Report to the project and use its default name of CrystalReport2.rpt. Follow the same steps as before:

  1. Add the following tables to the report:

    1. Customers
    2. Order Details
    3. Orders
    4. Products
  2. Choose the following fields to display:

    1. Customers.CompanyName
    2. Products.ProductName
    3. Order Details.Quantity
  3. Figure 11. After Configuration: The completed report with the chart.

  4. Group the report by Customers.CompanyName.
  5. For the Summaries section, ensure that only Sum of Order Details.Quantity is present.
  6. In the Chart dialog, check the Pie Chart option.

Figure 11 shows how the report looks after configuration.

Now, add a new Parameter Field to the report and name the parameter Product_Name. Click the Select Expert button and bind the parameter to the Products.ProductName field of the report.

Finally, add the following controls to Form1:

  • Label
  • ComboBox
  • Button
Add the following code in bold so that when the form is loaded, you will add the list of product names to the second ComboBox control:

Private Sub Form1_Load( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles MyBase.Load

        Dim connStr As String = 
           "Data Source=.\SQLEXPRESS;" & _
           "Initial Catalog=Northwind;" & _
           "Integrated Security=True"
        Dim sql As String = "SELECT CustomerID FROM Customers"
        Dim conn As SqlConnection = New SqlConnection(connStr)
        Dim comm As SqlCommand = New SqlCommand(sql, conn)
        Dim reader As SqlDataReader = comm.ExecuteReader
        While reader.Read
        End While

        sql = "SELECT ProductName from Products"
        comm.CommandText = sql
        reader = comm.ExecuteReader
        While reader.Read
        End While

    End Sub
Code the Click event of the View Chart button as follows:

Private Sub btnViewChart_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles btnViewChart.Click

        Dim report As New CrystalReport2
        report.SetParameterValue("Product_Name", ComboBox2.Text)
        With Form2
            .CrystalReportViewer1.ReportSource = report
        End With
Figure 12. Distribution of Orders: Viewing the orders for a product by the various customers in a pie chart.
End Sub
That's it! Press F5 to test the application. Figure 12 shows the distribution of "Genen Shouyu" ordered by the various customers.

Worth Further Investigation
In this article, you have seen the versatility offered by Crystal Report for Visual Studio 2005. What's been covered is just a small tip of the Crystal Report iceberg; it's definitely worth it to explore Crystal Report's other features. Let me know if this article is helpful to you and what other topics you want to see me cover in my future articles on Crystal Report.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date