Sunday, January 7, 2007

Walkthrough Crystal Report

How To: Create a Crystal Report from ADO.NET Dataset using Visual Basic .NET
Introduction:
Crystal Report of Visual Studio .NET is the standard reporting tool for Visual Studio .NET. You can host reports on web and windows platform and can publish reports as Report Web services on the web server. It is based on framework of Crystal Report 8.0 and uses open and flexible architecture, with standards like XML, to allow porting reports over the web. Using crystal report expert you can choose report layouts, display charts, calculate summaries, subtotals as grouped data as well as conditionally format text and rotate text objects.
Although Crystal Reports for Visual Studio .NET supports variety of data source like ADO recordset, CDO recordset, DAO recordset, MS Excel workbook, this walkthrough endeavor to explain How to report off ADO.NET DataSet using Visual Basic .NET.
As you all know DataSet is the core component of distributed application and is explicitly designed for data access independent of any data source. Dataset can be created from variety of sources. Whatever the source is, before reporting off ADO.NET DataSet you must perform the following task:
Ø Generate an object for the DataSet.
Ø Connect report to DataSet Object.
Ø Push data into DataSet Object.
Ø Bind report to Windows Forms Viewer to display report with actual data at runtime.
Requirements:
ü Visual Studio .NET 2002
ü .NET Framework 1.0
ü SQL Server 2000 with Northwind database

Generating an Object for the DataSet
Object for ADO.NET is a collection of dataset classes created in memory.
To create a dataset object from Northwind database in SQL Server, using ADO.NET DataSet Designer.
In the Solution Explorer, right-click the project name, point to Add, and click Add New Item.
In the Categories area of the Add New Item dialog box, expand the folder and select Data.
In the Templates area, select Dataset.
Accept the default name Dataset1.xsd.
This creates a new schema file that will be used to generate a strongly typed dataset. The schema file will be displayed in ADO.NET Dataset designer.
In the Solutions Explorer, click on Dataset1.xsd file, if now already the active view.
From the Server Explore, on the right connect to SQL Server and drill down to Northwind Database.
Highlight the Table Customers (or stored procedure if desired) and drag and drop it on the Interface of Dataset1.xsd. Dataset1.xsd should now be displayed in the Dataset tab as under
This creates a dataset object and contains only a description of the database based on the schema in Dataset1.xsd. It does not contain the actual data.

Connecting Report to an ADO.NET Dataset Object
From ADO.NET Dataset Object you can add tables to Crystal Report using Database Expert in Crystal Report Designer.
To create a new report and connect it to Dataset object which contains description for Customers table in Northwind database
In the Visual Studio .NET Solution Explorer, right-click your project to display the shortcut menu.
Point to Add and click Add New Item.
In the Add New Item dialog box, select Crystal Report from the Templates area. Click Open.
Crystal Report Gallery will be displayed, as shown below
You can choose from any of the options provided in Crystal Report Gallery. But for the purpose of this walkthrough choose As a Blank Report and click OK.
On File menu, click Save to save the report.
Right click in the Report Designer, point to Database, and click Add/Remove Database.
You’ll be presented with Database Expert wizard.
In the Database Expert wizard, expand the Project Data folder, expand the ADO.NET Datasets folder and select the dataset object as shown below
If you now drill down Database Fields node, in the Field Explorer, you can view Customers table and all its fields
Drag and drop the fields onto the report and format them as required.
Pushing data into DataSet object and binding report to Windows Forms Viewer
In order to display actual data in the report, you should fill the dataset object with the data before you bind the report to Windows Forms Viewer. You should do this in the corresponding source file for Windows Form.
Drag and drop CrystalReportViewer control on Form1 and set the DisplayGroupTree property to False, as shown below
Accept the default name as CrystalReportViewer1.
Open Form1 code editor and add the following code on Load event of Form1.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rpt As New CrystalReport1() 'The report you created.
Dim myConnection As SqlConnection
Dim MyCommand As New SqlCommand()
Dim myDA As New SqlDataAdapter()
Dim myDS As New Dataset1() 'The DataSet you created.

Try

myConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _
"Initial Catalog=northwind;")
MyCommand.Connection = myConnection
MyCommand.CommandText = "SELECT * FROM Customers"
MyCommand.CommandType = CommandType.Text
myDA.SelectCommand = MyCommand

myDA.Fill(myDS, "Customers")
rpt.SetDataSource(myDS)
CrystalReportViewer1.ReportSource = rpt

Catch Excep As Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Troubleshooting
Add reference to SqlClient namespace. Imports System.Data.SqlClient
Check connection to your server.
References
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/crystlmn/html/crconincorporatingreportsintoapplications.asp
Happy programming!!!

Web Technology links

http://support.businessobjects.com/communityCS/TechnicalPapers/cr10_UserGde_en.zip.asp
http://www.codeproject.com/aspnet/NonTypedCrystalReport.asp

http://www.codeproject.com/aspnet/crystal_report.asp?df=100&forumid=14950&exp=0&select=906105

RegularExpressions
http://www.4guysfromrolla.com/webtech/RegularExpressions.shtml

Passing Parameters from One Page to Another
http://aspnet.4guysfromrolla.com/articles/020205-1.aspx

AJAX
http://www.w3schools.com/ajax/default.asp