Modify Data at Run Time

How you modify report data (connection strings and SQL queries) depends on the report type you use. Let's take a look at each report type.

In Page and RDL Reports

To set up a new data source in a Page or RDL report at run time, that is, setting it all up in code, check out this topic in our User Guide.

Bind a Page Report to a Data Source at Run Time

But usually it's preferable to set up data sources at design time and then modify them at run time using code.

Tip: Setting up your data at design time allows you to drag fields onto the report and populates your fields list so you can avoid a lot of manual binding.

Designing reports is easier when you can drop down a list of all the available fields right in the data region. You can even drag fields from the Report Explorer right onto the report. Use the zero-based DataSources and DataSets collections for the report to access your design-time data in code. For example, the following code modifies the connection string and SQL query of your design-time DataSource and DataSet.

// C#  
private void Form1_Load(...)  
  // Instantiate the report.  
  GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport();  
  // Load a report definition file.   
  rpt.Load(new System.IO.FileInfo("PageReport1.rdlx"));  

  // Modify the connection string.  
  rpt.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";  
  rpt.Report.DataSources[0].ConnectionProperties.ConnectString =  
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Reels.mdb";   

  // Define the SQL string to use.  
  String tmpQuery = "Select Top 10 * From Movie";  
  // Modify the SQL string.  
  rpt.Report.DataSets[0].Query.CommandText =   

  GrapeCity.ActiveReports.Document.PageDocument pageDocument =   
    new GrapeCity.ActiveReports.Document.PageDocument(rpt);  
' VB.NET  

Private Sub Form1_Load(...) Handles MyBase.Load  
  ' Instantiate the report.  
  Dim rpt As New GrapeCity.ActiveReports.PageReport()  
  ' Load a report definition file.  
  rpt.Load(New System.IO.FileInfo("PageReport1.rdlx"))  

  ' Modify the connection string.  
  rpt.Report.DataSources(0).ConnectionProperties.DataProvider = "OLEDB"  
  rpt.Report.DataSources(0).ConnectionProperties.ConnectString _  
    = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Reels.mdb"  

  ' Define the SQL string to use.  
  Dim tmpQuery As [String] = "Select Top 10 * From Movie"  
  ' Modify the SQL string.  
  rpt.Report.DataSets(0).Query.CommandText = _   
    GrapeCity.ActiveReports.Expressions.ExpressionInfo.Parse(tmpQuery, _   

  Dim pageDocument As New GrapeCity.ActiveReports.Document.PageDocument(rpt)  
End Sub  

Important: When you use an RDL report with multiple DataSets, you may need to set the DataSets index to something other than 0. You can get the name of the DataSet using code like:


Also, note that the design-time list of fields is not modified when you set SQL strings at run time, so be sure that your design-time data set has the same fields as your run-time one (or manually bind report items to the fields).

In Section Reports

In a section report, you can set up a data source using the DataSource property. You can modify the data source of a report at run time, so long as you set the property before creating the report, that is, in the ReportStart event, or before calling the Run method. For specifics, see the following topics in our User Guide.

The following sample topic might be also helpful. It describes how to use data classes, such as a DataReader or DataTable, as your data source.


GrapeCity Developer Tools
comments powered by Disqus