To report off a data source at runtime

You can take data from any source such as a database, data stream, or flat file, and then at run time you can manipulate and apply it to a report. Data is retrieved using a Rowset object and stored in a DataSet object. This example shows you how to manipulate a Rowset object by removing records from a report.

Note: If you create a report that is based on a DataSet object, the data that is used is not automatically saved with the report. To populate the report with data the next time you open it, you must either re-apply the data source to see current data, or access a saved version of the data source for preview.
  1. Add the fields to the RowsetMetaData object.
    In this example the RowsetMetaData object is populated with result fields from the report. Any Field object can be used to populate the RowsetMetaData object.
    RowsetMetaData rowsetMetaData = new RowsetMetaData();
    Fields<IField> resultFields = rcd.getDataDefinition().getResultFields();
    rowsetMetaData.setDataFields(resultFields);
  2. Use the setRowsetBatchSize method of the RowsetController object to set the number of records to retrieve in each batch.
    This example sets the batch size to 10 records.
    rowsetController rowController = rcd.getRowsetController();
    rowController.setRowsetBatchSize(10);
  3. Create a RowsetCursor object using the createCursor method of the RowsetController object.
    RowsetCursor rowCursor = rowController.createCursor(null, rowsetMetaData);
  4. Retrieve the Rowset object using the RowsetCursor object.
    Retrieve an instance of the Rowset object using the getRowset method of the RowsetController object.
    Rowset modifiedRowset = rowCursor.getRowset();
  5. Navigate through the Rowset object to view or manipulate the data.
    In this example all batches except the first batch are deleted.
    RecordBatch recordBatch = new RecordBatch();
    RecordBatches recordBatches = modifiedRowset.getRecordBatches();
            
    for (int i = 1; i < recordBatches.size(); i++)
    {
      recordBatches.getRecordBatch(i).removeAll();
    }
            
    modifiedRowset.setRecordBatches(recordBatches);
  6. Add the modified Rowset object to a Rowsets collection.
    Rowsets rowsets = new Rowsets();
    rowsets.add(modifiedRowset);
  7. Add the Rowsets collection to a Dataset object.
    IDataSet dataSet = new DataSet();
    dataSet.setRowsets(rowsets);
  8. Set the table and table links information for the DataSet object.
    DatabaseController databaseController = rcd.getDatabaseController(); 
    dataSet.setTables(databaseController.getDatabase().getTables());
    dataSet.setTableLinks(databaseController.getDatabase().getTableLinks());
  9. Set the Dataset object as the data source for the report.
    databaseController.setDataSource(dataSet, "", "");
The report contains only the first batch of records.
Example: 
This sample retrieves a Rowset from the report, removes all batches except for the first batch, adds the modified Rowset object to a Dataset object, and then changes the report data source at runtime. The result is a report that contains only the first batch of records.
void setRuntimeDataSource(ReportClientDocument rcd) throws ReportSDKException
{ 
  RowsetMetaData rowsetMetaData = new RowsetMetaData();
  Fields<IField> resultFields = rcd.getDataDefinition().getResultFields();
  rowsetMetaData.setDataFields(resultFields);
        
  RowsetController rowController = rcd.getRowsetController();
  rowController.setRowsetBatchSize(10);
        
  RowsetCursor rowCursor = rowController.createCursor(null, rowsetMetaData);
  Rowset modifiedRowset = rowCursor.getRowset();
        
  RecordBatch recordBatch = new RecordBatch();
  RecordBatches recordBatches = modifiedRowset.getRecordBatches();
        
  for (int i = 1; i < recordBatches.size(); i++)
  {
    recordBatches.getRecordBatch(i).removeAll();
  }
        
  modifiedRowset.setRecordBatches(recordBatches);
  
  Rowsets rowsets = new Rowsets();
  rowsets.add(modifiedRowset);
        
  IDataSet dataSet = new DataSet();
  dataSet.setRowsets(rowsets);
        
  DatabaseController databaseController = rcd.getDatabaseController();
  dataSet.setTables(databaseController.getDatabase().getTables());
  dataSet.setTableLinks(databaseController.getDatabase().getTableLinks());
      
  databaseController.setDataSource(dataSet, "", "");
}
This list includes the classes used by the sample code:
  • com.crystaldecisions.sdk.occa.report.application.DatabaseController
  • com.crystaldecisions.sdk.occa.report.application.ReportClientDocument
  • com.crystaldecisions.sdk.occa.report.application.RowsetController
  • com.crystaldecisions.sdk.occa.report.application.RowsetCursor
  • com.crystaldecisions.sdk.occa.report.data.DataSet
  • com.crystaldecisions.sdk.occa.report.data.Fields
  • com.crystaldecisions.sdk.occa.report.data.IDataSet
  • com.crystaldecisions.sdk.occa.report.data.IField
  • com.crystaldecisions.sdk.occa.report.data.IRowset
  • com.crystaldecisions.sdk.occa.report.data.RecordBatch
  • com.crystaldecisions.sdk.occa.report.data.RecordBatches
  • com.crystaldecisions.sdk.occa.report.data.Rowset
  • com.crystaldecisions.sdk.occa.report.data.Rowsets
  • com.crystaldecisions.sdk.occa.report.data.RowsetMetaData
  • com.crystaldecisions.sdk.occa.report.lib.ReportSDKException
  • java.util.Iterator