Create a Crystal Report using a .NET DLL as the datasource

You can add reports to your application using the version of Crystal Reports that comes with VS.NET. Right click your project in Solution Explorer and select Add | Add New Item | Crystal Reports.

The DataSource for your report can be the .NET DLL that is used as your data access layer. To see how to use an XML Schema (XSD) file which describes the columns in your datatable, click here.

To create a DLL for use as a DataSource for a Crystal Report create a Class Library project in VS.NET. Add a Public Function that returns a DataTable for each Datatable that will be used in your reports.

A sample DLL written in VB.NET might look like:

    Imports System.Data.OleDb

    Public Class CRDAL

        Public Function Customers() As DataTable
            Try
                Dim strConnection As String = "..."
                Dim Connection As New OleDbConnection(strConnection)
                Dim DS As New DataSet
    
                Dim strSQL As String = "Select * From Customers"
                Dim DA As New OleDbDataAdapter(strSQL, Connection)
                DA.Fill(DS, "Customers")
        
                Return DS.Tables("Customers")

            Catch ex As Exception
                'some error handling
            End Try
        End Function

        Public Function Orders() As DataTable
            Try
                Dim strConnection As String = "..."
                Dim Connection As New OleDbConnection(strConnection)
                Dim DS As New DataSet
    
                Dim strSQL As String = "Select * From Orders"
                Dim DA As New OleDbDataAdapter(strSQL, Connection)
                DA.Fill(DS, "Orders")
    
                Return DS.Tables("Orders")

            Catch ex As Exception
                'some error handling
            End Try
        End Function

    End Class

In your Crystal Report you reference the DLL by opening the Field Explorer, right clicking Database Fields, selecting Database Expert, expanding the Create New Connection node, and the ADO.NET (XML) node then double clicking Make New Connection. At the ADO.NET (XML) dialog set the File Path to your DLL file by clicking the elipses and navigating to the DLL. Hit Finish when done.

The public functions or datatables exposed by your DLL will appear under the ADO.NET (XML) node in the Available Data Sources pane. Select the desired datatable within it and add it to the Selected Tables pane. Hit OK when done.

Now in the Field Explorer under the Database Fields node you will see your datatable and its fields which you can drag onto the report.

To learn how to run the Crystal report and display it in the CrystalReportViewer control visit my Crystal Report Viewer page.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page