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-10. Linking the dataset file to the report and previewing it.
Dim myReport as New CrystalReport1
Dim myDataSet As New DataSet()
'Use the appropriate FillDataSet() method from the previous section
FillDataSet(myDataSet)
'Uncomment the following line if you need to create a new dataset file
'CreateXmlFile("C:\FileName.ds", myDataSet)
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 passes it to the FillDataSet() method. This method is from the code listings in the prior section. Use the one that matches your data source and modify it for any special needs you have. There is also a call to the procedure WriteXmlFile() and it is commented out. As mentioned earlier, before you build a report you need to have a dataset file that the report can reference. It is commented out so that you can run it again if you need to rebuild the dataset file.

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 that table names are.