Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.18 DLLs and DataSets

Reporting from DataSets in a DLL

Crystal Reports also gives you the ability to report from DataSets that are within a separate DLL. In a multi-tier architecture, this lets you build reports directly from the data layer without having to write extra code to encapsulate the dataset in a local object variable.

The steps for doing this are almost identical to reporting from the IDataReader interface. When creating the report, use the DLL as the data source and select the proper class and method that returns the dataset object.

Let’s do a walk-through of the steps to report from a DLL that has a public method which returns a dataset. I assume that you already have a project library created, but for illustration purposes I’ll go through the steps of creating one from scratch so that you can test it on your computer.

The first step is to create a new project using the Class Library template. In this example, I named my project, MyDataSet. After creating the project, I deleted the default Class1.vb that was created automatically and added a class called DataSource. The code is in Listing 17-15. The class creates a public function called GetCustomersDS() which returns a DataSet object.

Listing 17-15. Source code for the MyDataSet project.
[VB.NET}
Imports System.Data
Public Class DataSource
Private Const CONNECTION_STRING_DS As String = _
"Data Source=vmware;Initial Catalog=xtreme;User Id=sa;Password=pw"
Private Const CUSTOMER_TABLE As String = _
"SELECT [Customer ID], [Customer Name], [Contact Last Name], City FROM Customer"
Public Function GetCustomersDS() As DataSet
Dim myDataSet As DataSet
Dim myAdapter As System.Data.SqlClient.SqlDataAdapter
myAdapter = New SqlClient.SqlDataAdapter(CUSTOMER_TABLE, CONNECTION_STRING_DS)
myDataSet = New DataSet()
myAdapter.Fill(myDataSet, "Customers")
Return myDataSet
End Function
End Class
[C#]
Using System.Data;

public class DataSource
{
//Note: Use your own connection string here
private const string CONNECTION_STRING = "provider=sqloledb;Data Source=vmware;Initial Catalog=xtreme;User Id=sa;Password=pw";
private const string CUSTOMER_TABLE = "SELECT [Customer ID], [Customer Name], [Contact Last Name], City FROM Customer";
public static IDataReader GetCustomers()
{
OleDbConnection myOleDbConnection = new OleDbConnection(CONNECTION_STRING);
myOleDbConnection.Open();
OleDbCommand myOleDbCommand;
myOleDbCommand = new OleDbCommand(CUSTOMER_TABLE, myOleDbConnection);
IDataReader myIDataReader = myOleDbCommand.ExecuteReader();
return myIDataReader;
}
}

After you enter this code, compile it so that it saves it as a DLL file. Now it’s ready to be reported on.

The second step is to create a project, or open an existing one, and add a report to it that consumes the DLL. When you create the report using the Report Wizard, on the Data tab, click the Create New Connections node and click the ADO.NET node. This opens the ADO.NET window where you can enter the connection information (refer back to Figure 17-5).

For the file path, click the ellipses button to open the Open File dialog box. Change the File Type dropdown to “.NET Data Provider DLL”. Navigate to where the DLL is located and select it. On the Class Name option, click the dropdown list and select the myDataSet.DataSource class.

Before clicking the Finish button, you need to tell Crystal Reports that the DLL has a method which returns a DataSet. Click the Use DataSet from Class checkbox. This causes a new dropdown box to appear which lists the methods in the class that return a dataset. Click the arrow and select the GetCustomerDS method.





Figure 17-6. Specifying the GetCustomerDS method as a data source.

Click the Finish button to save your changes and close the window.

The Data window now shows you the DLL as a data source and the GetCustomersDS method is shown as the table name. Double-click on the GetCustomersDS table so that it is added to the Selected Tables list.

Click the Next button to go to the Fields tab. Here you will see all the fields that from the DataSet are listed. Behind the scenes, Crystal Reports instantiated the DLL, called the GetCustomersDS method, and examined the data to determine the fields available.

At this point, creating the report follows the standard steps of using the Report Wizard to pick the fields to print and going through the remaining tabs.

After you have finished creating the report, it has stored an internal reference to the DLL. Printing the report is simply a matter of getting a reference to the report and passing it to the viewer. There is no need to call the SetDataSource() method. For example, here is sample code which opens the report and previews it.

Listing 17-16. Previewing the report that uses the DataReader as a data source.
[VB.NET]
Dim myReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
myReport.Load("..\..\CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = myReport
[C#]
CrystalDecisions.CrystalReports.Engine.ReportDocument myReport;
myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myReport.Load(@"..\..\CrystalReport1.rpt");
CrystalReportViewer1.ReportSource = myReport