We can use the custom collection to bind to the Gridview or any other databound control. In the same way we can use custom collections to create a crystal report. In this article we will demonstrate how to create report using custom collection.
We learned in the previous article, how to create report with dataset in ASP.NET. You can read the article using the link below:
Here first we need to create .NET Object to design report, therefore you need to design a database with a table.
The created database name is “HouseRental” with “HouseOwnerInfo” table. There are eight columns in the “HouseOwnerInfo.
Let us design a report to this table, to get owners summary report.
Note: The database design is only for demonstration purpose.
Next we need to create ASP.NET project with Visual Studio, let us open visual studio and click File -> new -> Project, then select the ASP.NET web Application from the template and give name as “CustomObjectReportDemo”.
Now we need to create a custom class called as ‘HouseOwnerInfo’. Now add following fields and properties to the class.
We have designed object for HouseOwnerInfor. We made this object from the columns of HouseOwnerInfo table.
Now we have database and custom .NET object. Remember our scenario creates report using the custom .net object. To that we need to do one more thing, which is we need XML structure for HouseOnwerInfo table to design report with .NET object.
Using the XML structure, you need to design crystal report. Now go to the SQL server 2005 and open new Query window, type followings lines.
And run the query , you get table structure as below XML format.
Next we need to copy this, paste in xml file and save. Then add to the project.
Let us start designing the report.
Right click on the project and select add new item, then select Crystal report from the list of item templates and give name of the report as “CustomObjectReportDemo.rpt”. Click ok button.
This is standard window to create report using design wizard. Here you can leave default selection and click ok button to process next step.
Here when you click “>>” button you will see a new window as follow.
This is a new place, when we select the .NET Object reporting, as we need to assign XML file to design report which has the same structure as the .NET Object. To that click on the ‘…’ button and locate the path of the XML file. The class name automatically picks the tree view node in crystal report previous window.
Now make sure that you have selected all the fields from the XML file. Then continue the wizard and finish it off. When you are finished with all the steps correctly, you will get the report as bellow.
The next step is the most important, since you need to make the collection of your custom object, which builds using database data.
Therefore you need to write the data access code to retrieve data from the database and build collection of HouseOwnerInfo.
Here we have written a simple data access code which reads the data from the reader, assigns to our custom object(HouseOwnerInfo), then finally add to the list and return as a list.
Now we have the list of houseOwnerInfo, next step, you need to setup your report. Before that you need to setup the crystal report viewer in your page as below.
Now we have setup crystal report viewer in the web page. We need to load report to report document.
Note: Here we do not need to set database logon Information to reportdocument.
Assign the data source to the report document
Finally you set report object to the report viewer to render the report on the page.
Complete code to load,set data source and render report on the page.
Now build the solution and run. If you don’t have any exception you will get result as:
1. When you build report with custom object, fields and objects of XML file should be same.
2. When you design report, you need XML structure file which is going to be use in report data.
3. Here you do not need to set database logon information to report.
In this article we learned, how to design report and build with custom .NET object.
- B.Sc. degree in Computer Science.
- 4+ years experience in C#.net,VB.net adn JAVA.
- Obsessed in OOP style design and programming.
- Designing and developing Network security tools.
- Designing and developing a client/server application for sharing files among users in a
way other than FTP protocol.
- Designing and implementing GSM gateway applications and bulk messaging.
- Windows Mobile and Symbian Programming
- Having knowledge with ERP solutions
The summary of my skills:
C#, VB.Net#,ASP.net, VC++, Java, WPF,WCF, SilverLight,Oracle,PL/SQL, SQL Server, MS Access
My Web Blogs