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:
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:
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”.