Web Listing 1: C# Code for Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace HierarchicalReport { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Connection string; substitute DataSource with your server name string cnString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI;"; // Declare connection, command, and other related objects SqlConnection conReport = new SqlConnection(cnString); SqlCommand cmdReport = new SqlCommand(); SqlDataReader drReport; DataSet dsReport = new dsEmployee(); try { //open connection conReport.Open(); // Prepare connection, get the data through the reader, and populate DataSet cmdReport.CommandType = CommandType.Text; cmdReport.Connection = conReport; cmdReport.CommandText = @"SELECT Person.Contact.FirstName + Person.Contact.LastName + ' / ' + HumanResources.Employee.Title AS EmployeeName, " + "HumanResources.Employee.ManagerID, HumanResources.Employee.EmployeeID " + "FROM HumanResources.Employee INNER JOIN Person.Contact ON " + "HumanResources.Employee.ContactID = Person.Contact.ContactID"; // Read data from command object drReport = cmdReport.ExecuteReader(); // Load data directly from the reader to the DataSet dsReport.Tables[0].Load(drReport); // Close reader and connection drReport.Close(); conReport.Close(); // Prepare report for preview rptHierarchical rptHierarchicalReport = new rptHierarchical(); rptHierarchicalReport.SetDataSource(dsReport.Tables[0]); crystalReportViewer1.DisplayGroupTree = false; crystalReportViewer1.ReportSource = rptHierarchicalReport; } catch (Exception ex) { // Display generic error message to user MessageBox.Show(ex.Message); } finally { // Check whether connection is still open; if so, attempt to close it if (conReport.State == ConnectionState.Open) { conReport.Close(); } } } } }