Search
Wednesday, August 20, 2008 ..:: Articles ::.. Register  Login
  
How To Create a Crystal Report off ADO.NET Dataset using Visual Studio.NET IDE
by Sai Panyam Last Updated:18 Oct 2004 View Count:1637 Rating:( 1.28/ 9 ) from 39 Vote(s)
Summary
Crystal Reports for Visual Studio.NET supports reports that access ADO.NET Datasets. You can create an ADO.NET dataset from a variety of sources. Whatever the source, before you can report off an ADO.NET data source, you must first generate an object for the dataset. After that, one can use the Report Expert in Crystal Reports for VS.NET to create a report based on the data description provided by the dataset object.
Creating a Crystal Report from within VS.NET IDE

In order to create a Report we need to first create a dataset. VS.NET has got a control called the “Report Viewer” to display the report at runtime. The walkthrough in this article shows you the steps involved in creating reports. The broad steps needed for this to occur are as follows:

v      First open VS.NET and drag and drop a “CrystalReportViewer” control on the ‘FORM1’.

v      Name it rpvReport in the properties pane.

v      Use the ADO.NET Dataset Designer to generate an ADO.NET dataset object from Northwind Database (Installed with SQL Server Installation by default)

v      Use the Standard Report Expert in Crystal Reports for VS.NET to create a new report in the same project based on the data description provided by that dataset object

v      Use the ADO.NET Object Model to push the data into the dataset object at runtime.

v      Use the Report Engine Object Model to pass the populated dataset object to the Report Engine, so that the report can display the actual data at runtime.

v      Bind the previously defined Report Viewer (rpvReport) to the report in application code.

 

Connecting a Report to an ADO.NET Dataset object

Use one of Crystal Report Experts (e.g. Standard Report Expert) to add tables from the ADO.NET dataset object.

Below you will create a new report and connect to the dataset object that has been created based on the Customers table in Northwind Database.

 

  1. In the Solutions Explorer, right click the project node and Select Add|Add New Item.
  2. From the Templates pane, Select CrystalReport and accept the default name of CrystalReport1.rpt and click Open.

 CrystalReport1.jpg

 

  1. You will be presented with a Crystal Report Gallery as under

 CrystalReportGallery.jpg

 

  1. You can create the report from any one of the given options. For the purposes of this walkthrough choose As Blank Report and click OK.
  2. You can now see the blank Crystal Report in the IDE.
  3. In the Field Explorer (on left) right click Database Fields and choose Add/Remove Database …
  4. You will be presented with a Database Expert Wizard.
  5. Drill down to the Dataset previously created from the Project Data node and move the Table Customers to Selected Tables pane and click OK.

 

 DBExpert.jpg

 

  1. Now if you expand the Database Fields Node in the Field Explorer, you can see the Customers table and all the fields in it.
  2. Drag and drop the fields onto the report and format them or position them. Add new text fields or logos etc. You can also add remove Groups and write formulas etc.
Setting up the report in the Report Viewer
Now the only thing left is to connect this managed report to the data source at runtime and also to the Report viewer.

1.       Open Form1 code window.

Add a Procedure ShowReport and PrepareReport Function.

VB.NET

Private Sub ShowReport()

Dim rpt As New CrystalReport1() 'The report you created

Dim dsReport As New Dataset1() 'The dataset that you created for this report

'We need a report object that is fully populated to display, so declare one

Dim objReport As Object

objReport = prepareReport(rpt, dsReport, "SELECT * FROM Customers")

rpvReport.ReportSource = objReport

End Sub

 

'To get a report from Table

Public Overloads Function prepareReport( ByVal rpt As Object , ByVal dsReport As Object , ByVal tblName As String ) As Object

Dim myConnection As SqlConnection

Dim MyCommand As New SqlClient.SqlCommand()

Dim MyDA As New SqlDataAdapter()

Try

myConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _

"Initial Catalog=northwind;")

MyCommand.Connection = myConnection

MyCommand.CommandText = tblName

MyCommand.CommandType = CommandType.Text

MyDA.SelectCommand = MyCommand

MyDA.Fill(dsReport, "Customers")

rpt.SetDataSource (dsReport)

Return rpt

Catch ReportErr As Exception

MessageBox.Show("Prepare Report Error:" & vbCrLf & ReportErr.Message, "Report Sample", MessageBoxButtons.OK, MessageBoxIcon.Error)

Finally

MyDA.Dispose()

MyCommand.Dispose()

myConnection.Dispose()

End Try

 

End Function

 

Troubleshooting

Common problems encountered would be improper SQL Server connection.

Check the connection to your server

 

Next Steps

The prepareReport function is an overloaded function. The reason being you can write other prepareReport functions for example like connecting to a stored procedure, with and without parameters and other complex reports like having multiple tables or multiple stored procedures. This article is a starting point for all your Crystal Reports adventures!!!!!!

 

I have scoured through a lot of Documentation and put this article together so that you can avoid all the hassles in trying to connect Crystal Reports through VS.NET

Happy programming!!
References

For further Information refer to these sites:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/crystlmn/html/crconincorporatingreportsintoapplications.asp

 

http://www.businessobjects.com/products/reporting/crystalreports/default.asp

Please rate the quality of this article
Poor
Outstanding
Tell me why you rated this content this way. (optional)
 
Rating Spread

  

Copyright 2002-2005 Sai Panyam   Terms Of Use  Privacy Statement