Connect a Dataset instantiaded by code to a CrystalReport reporting tool

Hi all,
In today’s post I want to introduce a simple method for attaching DataSet objects, instantiated in our code, to CrystalReport 2008 reporting tool.
CrystalReport is a powerful tool that allows you to easily create reports on data processed in our .NET applications.

The following steps are used to create the reports of a dataset created in our application:
1. First of all, add a new Form to our VisualStudio project. This form will be used to show the report (Add -> Windows Form)
2. Add in graphics mode, from the “toolbox” panel, a CrystalReportViewer object in the new Form just created
3. Add a new DataSource object to our project(Data -> Add new Datasource) with the same structure of the dataset that we want to represent. If we want to reporting data on a database, just select the tables and their attributes that must to be represented. A new file “xsd” will be created.
4. Add a CrystalReport object to our project. Right mouse click on our project icon and choose Add New Item -> Crystal Report. A modal window will be appears. Choose the standard model and, on the next page of the Wizard, select the dataset created in the previous point (CrystalReport will understand the data structure on which it must to work)
5. Proceed with the construction of the CrystalReport page using VisualStudio
6. Right click on the form created in step 1, click the “Choose a crystal report”, and select the report created in steps 4 and 5 in the new opened modal window.
7. Any .xsd file can be deleted from our project, XSD file is no longer required
8. Add the following code lines in the behind code of form created in step 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public partial class Frm_Prova : Form
    {
        public Frm_Prova(DataSet data)
        {
            InitializeComponent();
            CryReport1.SetDataSource(data);
        }

        //
        //           Rilascio della memoria alla chiusura dell'applicazione
        //

        private void Frm_Prova_FormClosed(object sender, FormClosedEventArgs e)
        {
            CryReport1.Dispose();
            this.Dispose();
        }

    }

where “CryReport1″ is the object instance of CrystalReport added in point 6.

9. For example, if you want to create the report of some data on a DB through the pushing of an ASP.NET Button, you must to add the following code lines in the Button’s Click event:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private void buttonPrintReport_Click(object sender, EventArgs e)
        {

        //Retrive DB data
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet Our_Data = new DataSet();
                string query;

                Our_Data.Tables.Add();

                query = "SELECT * FROM TABLE_NAME";
                adapter.SelectCommand = new SqlCommand(query, DBConnection);

                adapter.Fill(Our_Data.Tables[0]);
                Our_Data.Tables[0].TableName = "TABLE_NAME";

                Frm_Prova Report = new Frm_Prova(Our_Data);
                Report.ShowDialog(this);

        }

In this case, we had to create an XSD file with the structure of the table “TABLE_NAME” at point 3.

Pushing the “buttonPrintReport” Button, on screen will be appear a form containing the print preview of our report, representing the data contained in our dataset “Our_Data”.