Object-Oriented Report Development using Crystal Reports and ASP.NET 2.0 - Adding a Factory class to populate object collection
(Page 4 of 4 )
This class mainly interacts with the "DBHelper" class to fetch information from the database and populates the collection objects. This class can also have DML operations like Insert, Update, Delete, and so forth. For the sake of clarity, I removed those methods.
Add a new class (OrderFactory.vb), which deals with business logic, and copy the following code:
Imports Microsoft.VisualBasic
Imports System.Data
Public Class OrderFactory
Public Function GetOrdersListCollection() As OrderCollection
Dim clnOrders As New OrderCollection
For Each dr As DataRow In GetOrdersList.Rows
clnOrders.Add(New Order(dr("OrderID"), dr("CustomerName"), dr
("EmployeeName"), dr("OrderDate"), dr("OrderValue")))
Next
Return clnOrders
End Function
Public Function GetOrdersList() As DataTable
Dim sql As String
sql = "SELECT dbo.Orders.OrderID, dbo.Customers.CompanyName AS
CustomerName, dbo.Employees.LastName AS EmployeeName,
dbo.Orders.OrderDate, "
sql &= " SUM(dbo.[Order Details].UnitPrice * dbo.[Order Details].Quantity)
AS OrderValue "
sql &= "FROM dbo.Orders INNER JOIN "
sql &= " dbo.[Order Details] ON dbo.Orders.OrderID = dbo.[Order
Details].OrderID INNER JOIN "
sql &= " dbo.Employees ON dbo.Orders.EmployeeID =
dbo.Employees.EmployeeID INNER JOIN "
sql &= " dbo.Customers ON dbo.Orders.CustomerID =
dbo.Customers.CustomerID "
sql &= "GROUP BY dbo.Orders.OrderID, dbo.Customers.CompanyName,
dbo.Employees.LastName, dbo.Orders.OrderDate"
Return DBHelper.getDataTable(sql)
End Function
End Class
If you would like to work with stored procedures, create a stored procedure at SQL Server and work with "DBHelper.SPgetDataTable("<storedprocedurename>")". This is another professional approach with the best database performance in mind.
Designing, developing and binding Crystal Report to the Visual Basic 2005 business logic class
Once the classes are complete, add a new Crystal Report to the web site (SampleReport01.rpt). With the Standard Report expert, select the "Order" class available under ".NET Objects" and include all the fields. Report Designer gets opened with all the fields and you can format the report according to your requirements.
Drag a button and CrystalReportViewer control onto the web form. In the code behind, copy the following code:
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
CrystalReportViewer1_Navigate(Nothing, Nothing)
End Sub
Protected Sub CrystalReportViewer1_Navigate(ByVal source As Object, ByVal e
As CrystalDecisions.Web.NavigateEventArgs) Handles
CrystalReportViewer1.Navigate
Dim rep As New ReportDocument
rep.Load(Server.MapPath("SampleReport01.rpt"))
rep.SetDataSource((New OrderFactory).GetOrdersListCollection)
Me.CrystalReportViewer1.ReportSource = rep
Me.CrystalReportViewer1.DataBind()
End Sub
End Class
The most important statement to note from the above code is the following:
rep.SetDataSource((New OrderFactory).GetOrdersListCollection)
I am creating an instance of "OrderFactory" and executing the "GetOrderListCollection" method. It returns the collection which gets bound to the report.
Finally, execute the application and hit the button to view the report.
I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |