Skip to main content

Event Driven Queries

Version: 7.0

Event Driven Queries

ReportWriter provides built in use of the most commonly used database types; Microsoft SQL Server, ODBC, OLE DB and Oracle. Any database type can however be used even if not of one of the four previously mentioned types. This is accomplished with the OpeningRecordSet event off of the Query object. In the OpeningRecordSet event, any database connection and adapter can be used then the specific DataTable (or DataReader) be used to create a DataTableRecordSet object (or DataReaderRecordSet for DataReader) and that can be passed directly into the RecordSet to be used for the report.

The following code example demonstrates how to specify and code an event handler for the OpeningRecordSet event of the Query object in order to dynamically specify a DataTable as the RecordSet to use:

public void CreateDataTableExample()
{
DocumentLayout report = new DocumentLayout( filePath );
Query query1 = report.GetQueryById( "Query1" );
query1.OpeningRecordSet += new OpeningRecordSetEventHandler( Query_OpeningRecordSet );
Document document = report.Run();
document.Draw( @"C:\Temp\report.pdf" );
}

private void Query_OpeningRecordSet( object sender, OpeningRecordSetEventArgs e )
{
EventDrivenQuery query = (EventDrivenQuery )sender;
SqlConnection connection = new SqlConnection( query.ConnectionString );
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter( "SELECT * FROM Products", connection );
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill( dataTable );
e.RecordSet = new DataTableRecordSet( connection, dataTable );
}
Sub ReportExample()
Dim Report As DocumentLayout = New DocumentLayout( filePath )
Dim MyQuery As Query = Report.GetQueryById("Query1")
AddHandler MyQuery.OpeningRecordSet, AddressOf Query_OpeningRecordSet
Dim MyDocument As Document = Report.Run()
MyDocument.Draw("C:\Temp\report.pdf")

End Sub

Private Sub Query_OpeningRecordSet(ByVal sender As Object, ByVal e As OpeningRecordSetEventArgs)
Dim MyQuery As EventDrivenQuery = CType(sender, EventDrivenQuery )
Dim MyConnection As SqlConnection = New SqlConnection(MyQuery.ConnectionString)
Dim MySqlDataAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Products", MyConnection)
Dim MyDataTable As DataTable = New DataTable
MySqlDataAdapter.Fill(MyDataTable)
e.RecordSet = New DataTableRecordSet(MyConnection, MyDataTable)
End Sub