Click here to Skip to main content
15,898,035 members
Articles / Mobile Apps

Reporting Hierarchical Recursive Data using MS Reporting Services

Rate me:
Please Sign up or sign in to vote.
4.60/5 (15 votes)
13 Jun 20068 min read 126.3K   1.1K   76  
An article to demonstarte the use of Reporting Services using Smart Client interfaces.
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;
using Microsoft.Reporting.WinForms;

namespace RecursiveData
{
    public partial class frmReportViewer : Form
    {
        public frmReportViewer()
        {
            InitializeComponent();
        }

    private void Form1_Load(object sender, EventArgs e)
    {
        //declare connection string
        string cnString = @"Data Source=(local);Initial Catalog=northwind;" +
                           "User Id=northwind;Password=northwind";

        //use following if you use standard security
        //string cnString = @"Data Source=(local);Initial Catalog=northwind;
        //                    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 object to get the data through reader and populate into dataset
            cmdReport.CommandType = CommandType.Text;
            cmdReport.Connection = conReport;
            cmdReport.CommandText = "Select FirstName + ' ' + Lastname AS EmployeeName, EmployeeID, ReportsTo From Employees";

            //read data from command object
            drReport = cmdReport.ExecuteReader();

            //new cool thing with ADO.NET... load data directly from reader to dataset
            dsReport.Tables[0].Load(drReport);

            //close reader and connection
            drReport.Close();
            conReport.Close();

            //provide local report information to viewer
            reportViewer.LocalReport.ReportEmbeddedResource = "RecursiveData.rptRecursiveData.rdlc";

            //prepare report data source
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "dsEmployee_dtEmployee";
            rds.Value = dsReport.Tables[0];
            reportViewer.LocalReport.DataSources.Add(rds);

            //load report viewer
            reportViewer.RefreshReport();
        }
        catch (Exception ex)
        {
            //display generic error message back to user
            MessageBox.Show(ex.Message);
        }
        finally
        {
            //check if connection is still open then attempt to close it
            if (conReport.State == ConnectionState.Open)
            {
                conReport.Close();
            }
        }
    }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect FeatherSoft Inc.
Canada Canada
Asif Sayed has over twenty + years experience in software development and business process architecture. He has a consulting firm in Toronto, Canada. His firm provides IT solutions to all sizes of industries. He also teaches .NET technologies at Centennial College in Scarborough, Ontario. Recently he has become member of team as a subject matter experts with Microsoft's Learning Division. He has a book published by Apress with the Title "Client-Side Reporting with Visual Studio in C#".

My blog: http://www.dotnetsme.com
My Website: http://www.feathersoft.ca

Comments and Discussions