Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.15 Binding the DataSet

Bind the DataSet to the report

The last step in using the Push Model is writing the code to link the fully populated dataset to the report. The ReportDocument class has a SetDataSource() method that takes a populated dataset object and uses it as the data source for the report. After calling this method the report is bound to the dataset and is able to print the records.

Listing 17-9. Linking the dataset file to the report and previewing it.
[VB.NET]
Dim myReport as New CrystalReport1
Dim myDataSet As New DataSet()
'Populate myDataSet with the appropriate data (see Listing 17-7 for an example)
…..
myReport.SetDataSource(myDataSet)
CrystalReportViewer1.ReportSource = myReport
[C#]
CrystalReport1 myReport = new CrystalReport1();
DataSet myDataSet = new DataSet();
//Populate myDataSet with the appropriate data (see Listing 17-7 for an example)
…..
myReport.SetDataSource(myDataSet);
CrystalReportViewer1.ReportSource = myReport;

The code first creates an instance of the report and assigns it to the myReport object variable. Then it creates a new DataSet object and populates it. Again, you can populate the dataset using the appropriate data source for your application. The SetDataSource() method is passed the populated dataset. This binds the data to the report.

It is common to bind the report to the dataset and get the error “Logon Failed”. This occurs when the table name wasn’t specified when the dataset was populated with the data table. It can also occur when the table name doesn’t match what the report is expecting. Examine the dataset object in debug mode and see what those table names are. It is also helpful to pass the datatable object to the report rather than the dataset. For example, you can pass myDataSet.Tables(0) or myDataSet(“tablename”) instead.

As mentioned earlier, I’ve been saying that you have to pass the report a dataset object to simplify the explanations in this section. You can also pass theSetDataSource() method a DataTable object or even a DataView object.

When using an XML data source that has date fields, Crystal Reports will not print the dates correctly on the page. There is some type of bug with how Crystal Reports interprets the dates in a dataset that is derived from an XML file. It can’t format the output correctly. The workaround for this bug is to read the XML schema into the dataset object prior to reading the XML data. Here is an example:

myDataSet.ReadXmlSchema("SampleData.xsd")
myDataSet.ReadXml("SampleData.xml")

Printing from a DataView object is the same as printing from a DataSet object. Since a dataview is based on an existing dataset, build the report using the dataset. Pass this dataset to the report’s SetDataSource() method. Here is a sample that would be used to filter the data in a dataset.

Dim myDataView As DataView
myDataView = New DataView(myDataSet.Tables(0), "CustomerId=123", "", DataViewRowState.CurrentRows)
myReport.SetDataSource(myDataView)

If you want to print the data from a datagrid, follow the same steps for printing a dataset. However, you first have to get the data from the datagrid and cast it as a dataset:

[VB.NET]
myDataSet = CType(myDataGrid.DataSource, DataSet)
[C#]
myDataSet = (DataSet)myDataGrid.Datasource