Home > c#, Visual Studio 2005 > Using the same DataSource in ActiveReports

Using the same DataSource in ActiveReports

April 25, 2007

Well, apparently I learned a few bad habits.  When I started using ActiveReports, it was out of necessity—a app we had outsourced was created using it and I, for the most part, followed subsequent development using that model.

Well, one thing that always bugged me focused on how reports and subreports seemed to constant recreate the same data over… and over… and over.  So, in a rare glimmer of free time, I set out to figure out a better way.

Note: This does not say this is the BEST way, but it seems a LOT better than what I was doing.

The “Old” Way

In the parent report, each subreport queried, via the BLL, the information necessary (See Shapes… how much simplier can we get…?)

subMath_Shapes Shapes = new subMath_Shapes();
Shapes.DataSource = BusinessLogic.Reports.GetReportResponsesHistorybySectionIdReportId(2, _reportid);
Shapes.ReportId = _reportid;
subMathShapes.Report = Shapes;

In the subreport, we requeried again in ReportStart and iterated through it in DataInitialize and detail_Format events.

private void subMathematics_ReportStart(object sender, EventArgs e)
    {
        _Shapes = BusinessLogic.Reports.GetReportResponsesHistorybySectionIdReportId(2, _reportid);
    }

private void subMathematics_DataInitialize(object sender, EventArgs e)
    {
        foreach (DataColumn column in _Shapes.Columns)
        {
            Fields.Add(column.ColumnName);
        }
    }

private void detail_Format(object sender, EventArgs e)
    {

      etc..

    }

Yeah, so, that’s not real smart; but it’s how I learned to do it.  Well, in this case, immitation was the wrong way to go.

The “New” Way

For the master report, I create a “master” table of all information and parse EACH with DataViews.  One single query to the database.

DataTable dt = new DataTable(); 

 int[] sections = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; 

 foreach (int section in sections) 
 { 
     dt.Merge(BusinessLogic.Reports.GetReportResponsesHistorybySectionIdReportId(section, _reportid), true); 
 }

subMath_Shapes Shapes = new subMath_Shapes(); 

Shapes.DataSource = new DataView(dt, "int_sectionid = 2", "int_questionid", DataViewRowState.CurrentRows); 

Shapes.ReportId = _reportid; 

subMathShapes.Report = Shapes; 

 

Now, in the subreport, simply reuse that DataSource object in the DataInitialize event.

if (DataSource != null) 
{ 
  DataView dv = DataSource as DataView; 
  foreach (DataColumn column in dv.Table.Columns) 
  { 
    Fields.Add(column.ColumnName); 
  } 
}

Before and after is almost unimaginable for the speed differences, though it is a bit embarassing not with not spending the time to UNDERSTAND how ActiveReports worked to begin with… (though I make no claims to understand it now).

Tags:

Categories: c#, Visual Studio 2005
  1. Luc
    April 25, 2007 at 10:40 pm

    It’s always cool to see how people use the product… Each report (and sub report) is just an ‘object’ and therefore developers (ie. like yourself) are able to extend it however you see fit (your own events, methods, properties, etc…). Great optimization tip!! Thanks for the post!

    Lucas Hardbarger
    Data Dynamics

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: