Thursday, March 25, 2010

Active Reports - Subreport data cut off

I call this experience a lesson learned. Active Reports warns of not touching controls in the Fetch_Data event, but I ignored it thinking a subreport isn't a control! And it caused me nothing but heartache to learn this lesson.

At first the report ran fine, I did not see the problem until I had alot more data to load, then I could see the subreport getting cut off. From looking at the report, it was clear the next report was being started, even though the first one's subreport data was not complete.

This is kind of how my report looked:

Library Name: My Library
Author Name: Joe Smith

Books
------------------------
My First Book
  Rating A: X
  Rating B: X
My Second Book
  Rating A: X
  Rating B: X
My Third Book
  Rating A: X
  Rating B: X
Total Rating: X

Page 1 of 3
**********************************

Library Name: My Library
Author Name: Pretend Name

Books
------------------------
My First Book
  Rating A: X
  Rating B: X
My Second Book
  Rating A: X
  Rating B: X
My Third Book
  Rating A: X <----DATA CUT OFF HERE!!!!


Page 2 of 3
**********************************

Library Name: My Library
Author Name: Ellen Jones

Books
------------------------
My First Book
  Rating A: X
  Rating B: X
My Second Book
  Rating A: X
  Rating B: X
My Third Book
  Rating A: X
  Rating B: X
Total Rating: X

Page 3 of 3
**********************************

This is sort of what I had in the code:


void report_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport.FetchEventArgs eArgs)
{
  if(eArg.EOF==false)
   {
    int id = Convert.ToInt32(Fields["ID"].Value.ToString());

    //get some data
    List list = SomeCallToGetData(id);

    ReportDetail rd = new ReportDetail();
    this.subReport1.Report = rd;
    this.subReport1.Report.DataSource = list;
   }
}

The problem was that the Fetch_Data would spin through all the records, and this would not allow the subreport to be properly written out.

So to resolve my problem, I followed the instruction of "don't touch controls in the Fetch Data" and moved this logic to the Detail_Format - not sure if this the right way, but something lead me to believe this was the right....

Now, I use the Fetch_Data to gather all the data, then store the subreport data in a private class variable called '_list' for use in the Format_Detail like this:

public partial class SummaryReportAR6 : DataDynamics.ActiveReports.ActiveReport
{

  private List _list;

  public SummaryReportAR6 ()
   {
    InitializeComponent();
    this.FetchData += new FetchEventHandler(report_FetchData);
    this.detail.Format += new EventHandler(Detail_Format);
   }

  void report_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport.FetchEventArgs eArgs)
  {
    if(eArg.EOF==false)
    {
     int id = Convert.ToInt32(Fields["ID"].Value.ToString());

     //get some data, store for later use
     _list = SomeCallToGetData(id);

   }
  }

  void Detail_Format(object sender, System.EventArgs e)
  {
     //set report datasource
     ReportDetail rd = new ReportDetail();
     this.subReport1.Report = rd;
     this.subReport1.Report.DataSource =      _list;
  }

}

hmmmm..I'm not really sure how the timing works out on this, but it seems to be working.

I will consider this one fixed until I see more problems.

Hope that helps someone out there.

5 comments:

  1. This will work. Some potential optimizations:
    1: Initialize this.subreport1.Report in the Report_Start event. You can initialize it only once this way rather than for each record.

    You could also do the "SomeCallToGetData" in the Detail_Format event and set it to the subreport directly rather than relying on the sequence of events in the Fetch_Data and Detail_Format and having to store the data in a instance field.

    Hope this helps, and thanks for posting about your experience with ActiveReports!

    ReplyDelete
  2. I'm not sure about anyone else, but I'm listening, and your posts have definitely been helpful! I've just started using ActiveReports and any insight is good to have. Thanks!!

    There's also a link to your article in the latest GrapeCity newsletter - way to go!

    Another girl in the computer industry...

    ReplyDelete
  3. @GinnyI'm so glad I can help someone, if with just one little error.

    I have not seen the GrapeCity newsletter that is great!

    ReplyDelete
  4. @Ginny Oh, yes, and I forgot to mention, it is so nice to meet 'another girl in the computer industry' :)

    ReplyDelete
  5. I thought the same thing when I found your site. :)

    ReplyDelete