Create a Crystal Report based on an xml schema (xsd) file



Download 81.92 Kb.
Page1/3
Date29.01.2021
Size81.92 Kb.
  1   2   3

Create a Crystal Report based on an XML schema (XSD) file

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 based on an XML Schema (XSD) file. Schemas files are XML files that describe the columns in your datatable. (To see how to set the DataSource to a .NET DLL click here). The easiest way to create a schema for your data is to call your dataset's WriteXmlSchema method:

Imports System.Data.OleDb
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")



strSQL = "Select * From Orders"

DA = New OleDbDataAdapter(strSQL, Connection)

DA.Fill(DS, "Orders")
DS.WriteXmlSchema("C:\SomeFolder\MySchema.xsd")

To add the schema to your solution right click your project in Solution Explorer and select Add | Add Existing Item and navigate to your .XSD file.

In your Crystal Report you reference the schema 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 XSD file by clicking the elipses and navigating to the schema file. Hit Finish when done.

Your schema 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.

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.

Pass parameters into a Crystal Report from VB.NET

You can programmatically pass parameter values into your Crystal Report using VB.NET code similar to this:

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

'

' Load the selected report file.

'

Dim CR As New ReportDocument

CR.Load(strReportPath)

'

' Declare the parameter related objects.

'

Dim crParameterDiscreteValue As ParameterDiscreteValue

Dim crParameterFieldDefinitions As ParameterFieldDefinitions

Dim crParameterFieldLocation As ParameterFieldDefinition

Dim crParameterValues As ParameterValues

'

' Get the report's parameters collection.

'

crParameterFieldDefinitions = CR.DataDefinition.ParameterFields

'

' Set the first parameter

' - Get the parameter, tell it to use the current values vs default value.

' - Tell it the parameter contains 1 discrete value vs multiple values.

' - Set the parameter's value.

' - Add it and apply it.

' - Repeat these statements for each parameter.

'

crParameterFieldLocation = crParameterFieldDefinitions.Item("StartDate")

crParameterValues = crParameterFieldLocation.CurrentValues

crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue

crParameterDiscreteValue.Value = strStartDate

crParameterValues.Add(crParameterDiscreteValue)

crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

'

' Set the Crytal Report Viewer control's source to the report document.

'

CrystalReportViewer.ReportSource = CR

If your report's DataSource is a .NET DLL DO NOT set the ReportDocument's DataSource property programatically as in the code below. However, if the DataSource is an XML Schema (.xsd) file you must set it prior to setting your parameters.

cr.SetDataSource(DS.Tables("Customer"))

If your report's DataSource is a .NET DLL as shown here and the public functions that return the datatables available to your report where changed to require parameters, as illustrated below, the parameters can be set as just described.

Public Function Customers(theStartDate As String) As DataTable

If the parameters were added to your Crystal Report by clicking the Parameter Fields node in the Crystal IDE's Field Explorer they can be set using the following code:

Dim cr As New ReportDocument

cr.Load(strReportPath)

cr.SetDataSource(DS.Tables("Customers"))

cr.SetParameterValue("StartDate", strMyParmValue)

CrystalReportViewer.ReportSource = cr

Run a Crystal Report from .NET using the Crystal Report Viewer

After creating reports using the lite version of Crystal Reports that comes with .NET or the full version you can display them using the CrystalReportViewer control.

Add CrystalDecisions references to your project and drag the ReportViewer control onto your form. The references may vary but typically include:

CrystalDecisions.CrystalReports.Engine

CrystalDecisions.Shared

CrystalDecisions.Windows.Forms

CrystalDecisions.ReportSource




Share with your friends:
  1   2   3




The database is protected by copyright ©essaydocs.org 2020
send message

    Main page