Introduction: I thought why we can't use the custom objects collection to generate report in ASP.NET. We can use the custom collection to bind to grid view or like any other tabular control. So there should be a way to create report with custom object collection. Implementation The purpose of this article is to design and build report from the .NET Custom objects. We learned in my previous article, how to create report with dataset in ASP.NET to read.http://www.highoncoding.com/Articles/201_Creating_Crystal_Reports_Using_Typed_DataSet.aspx. 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 "CustomOjbedctReportDemo". 
Now we need to create a custom class called as 'HouseOwnerInfo', to this add class file to the 'CustomObjectReportDemo'. Now add following fields and properties to the class. {codecitation class="brush: c#; gutter: true;" width="700px"} using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CustomOjbedctReportDemo { public class HouseOwnerInfo { #region Variables int id; string fName; string lName; DateTime dateofPurchase; string hPhone; string mPhone; string residencyStatus; #endregion #region Cons public HouseOwnerInfo() { } #endregion #region Properties public int Id { get { return id; } set { id = value; } } public string FirstName { get { return fName; } set { fName = value; } } public string LastName { get { return lName; } set { lName = value; } } public DateTime DateofPurchase { get { return dateofPurchase; } set { dateofPurchase = value; } } public string HomePhone { get { return hPhone; } set { hPhone = value; } } public string MobilePhone { get { return mPhone; } set { mPhone = value; } } public string ResidencyStatus { get { return residencyStatus; } set { residencyStatus = value; } } #endregion } } {/codecitation} Fine, we have designed object for HouseOwnerInfor. We made this object from the columns of HouseOwnerInfor table as field. Now we have database and custom .NET object. Remember our scenario again, creates report using the custom .net object. To that we need to do one more thing, which is we need XML structure for HouseOnwerInfor 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. SELECT [OwnerID] ,[FName] ,[LName] ,[DateOfPurchase] ,[PlanType] ,[HPhone] ,[MPhone] ,[RStatus] FROM [HouseRental].[dbo].[HouseOwnerInfo] for XML AUTO,Elements And run the query , you get table structure as below XML format. {codecitation class="brush: c#; gutter: true;" width="700px"}
0001 John Peter 2009-03-20T00:00:00 HDC 678965443 678965443 PR {/codecitation} Next we need to copy this, paste in xml file and save. Then add to the project. Let us start report designing. Right click on the Project and select add new item, then select Crystal report from the List of item template and give name of the report as "CustomObjectReportDemo.rpt". Click ok button. 
This is standard window of create report 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 .NET Object. To that click on the '...' button and locate the path of the XML file. The class name automatically pick from the tree view node in crystal report previous window. Now make sure whether you have selected all the fields from the XML file. Then continue the wizard to and finish it off. When you finish all the steps correctly, you will get the report as bellow. 
The next steps are most important and newer than earlier report articles, since you need to make the collection of your custom object, which builds using database data. Therefore you need to write a data access code to retrieve data from the database and build collection of HouseOnwerInfo. Code: {codecitation class="brush: c#; gutter: true;" width="700px"} public List<HouseOwnerInfo> GetAllHouseOnwersInfor() { try { List<HouseOwnerInfo> collectionOfHouseOwners = new List<HouseOwnerInfo>(); using (SqlConnection connection = new SqlConnection(WebConfigurationManager.AppSettings["Connection"])) { HouseOwnerInfo houseOwnerInfo; if (connection.State!=System.Data.ConnectionState.Open) { connection.Open(); } using (SqlCommand cmd = connection.CreateCommand()) { cmd.CommandText = "Select * from HouseOwnerInfo;"; cmd.CommandType = System.Data.CommandType.Text; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { houseOwnerInfo = new HouseOwnerInfo(); if (reader["OwnerID"] != DBNull.Value) { houseOwnerInfo.OwnerID = int.Parse(reader["OwnerID"].ToString()); } if (reader["FName"] != DBNull.Value) { houseOwnerInfo.FName = reader["FName"].ToString(); } if (reader["LName"] != DBNull.Value) { houseOwnerInfo.LName = reader["LName"].ToString(); } if (reader["DateOfPurchase"] != DBNull.Value) { houseOwnerInfo.DateofPurchase = DateTime.Parse(reader["DateOfPurchase"].ToString()); } if (reader["HPhone"] != DBNull.Value) { houseOwnerInfo.HPhone = reader["HPhone"].ToString(); } if (reader["MPhone"] != DBNull.Value) { houseOwnerInfo.MPhone = reader["MPhone"].ToString(); } if (reader["RStatus"] != DBNull.Value) { houseOwnerInfo.MPhone = reader["RStatus"].ToString(); } collectionOfHouseOwners.Add(houseOwnerInfo); } } } } return collectionOfHouseOwners; } catch (Exception ex) { throw ex; } } {/codecitation} Here we have written a simple data access code, in the middle lines of the code read data from that reader, assign to our custom object(HouseOwnerInfo), then finally add to list and return as list. Now we have the list of houseOwnerInfo, next step, you need to set to your report. Before that you need setup the crystal report viewer in your page as below. Html Code: {codecitation class="brush: c#; gutter: true;" width="700px"} {/codecitation} Now we have setup crystal report viewer in the web page, now we need to load report to report Document. {codecitation class="brush: c#; gutter: true;" width="700px"} ReportDocument report = new ReportDocument(); report.Load(Server.MapPath("CustomObjectReportDemo.rpt")); {/codecitation} Note: Here we do not need to set database logon Information to reportdocument. But you need to set data source to report as follow. {codecitation class="brush: c#; gutter: true;" width="700px"} report.SetDataSource(new DataBaseManager().GetAllHouseOnwersInfor()); {/codecitation} 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. Code: {codecitation class="brush: c#; gutter: true;" width="700px"} protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BiulReport(); } } private void BiulReport() { ReportDocument report = new ReportDocument(); report.Load(Server.MapPath("CustomObjectReportDemo.rpt")); report.SetDataSource(new DataBaseManager().GetAllHouseOnwersInfor()); cryreportViewer.ReportSource = report; } {/codecitation} Everything is fine, now build the solution and run. If you don't have any exception you will get result as: 
Key Points: 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. Conclusion In this article we learned, how to design report and build with custom .NET object. |