This is a migrated thread and some comments may be shown as answers.

Cannot set datasource for report at runtime

4 Answers 170 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Randy
Top achievements
Rank 1
Randy asked on 01 Nov 2012, 07:26 PM
I'm new to reporting so maybe I'm missing something basic here.

I have a .NET web application. I have a ReportViewer tied to a Report on a page. At runtime I want to programatically assign a LinqToSql query to the report control. I've tried doing this (see code) but I get the following error on the report viewer:
An error has occurred while processing Report 'ARQSort': Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'.


namespace CMS
{
    public partial class ARQSort : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CMS.Reports.ARQSort report1 = new CMS.Reports.ARQSort();
 
            using (CMSDatabaseDataContext myCMSDataContext = new CMSDatabaseDataContext())
            {
 
                IEnumerable<arq_recipient> arqSortQuery = (from a in myCMSDataContext.arq_recipients
                                                      where a.fdc_accepted == true
                                                      select a);
 
             
                // Assigning the ObjectDataSource component to the DataSource property of the report.
                report1.DataSource = arqSortQuery;
                 
                Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
                instanceReportSource.ReportDocument = report1;
                ReportViewer1.ReportSource = instanceReportSource;
 
            }
 
        }
   }
}


What am I missing?

4 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 05 Nov 2012, 07:49 PM
Hello Randy,

In the provided code we have noticed that you are disposing the context immediately after setting it to the report definition. That is prior the report data binding completion. Thus we have two suggestions:
  • Transfer all the data from the datacontext to a dataset and bind the report to the dataset as elaborated in the How to: Bind to a DataSet help article.
  • Another option is to handle the report ItemDataBound and then to Dispose your datacontext after all the report binding is completed.
Kind regards,
Peter
the Telerik team

HAPPY WITH TELERIK REPORTING? Do you feel that it is fantastic? Or easy to use? Or better than Crystal Reports? Tell the world, and help fellow developers! Write a short review about Telerik Reporting and Telerik Report Designer in Visual Studio Gallery today!

0
Randy
Top achievements
Rank 1
answered on 05 Nov 2012, 08:55 PM
I've always encapsulated my database contexts within the "Using" section. How would I dispose of the current context in a separate event? ie-how do I get the current context in the ItemDataBount event of the report so I can dispose there?
0
Accepted
Steve
Telerik team
answered on 06 Nov 2012, 03:05 PM
Hi,

As my colleague suggested you can consider transferring your data to another data type and bind the report this way. Additionally I believe that invoking .ToList() to the LINQ query should resolve the problem.

Let us know how it goes.

Greetings,
Steve
the Telerik team

HAPPY WITH TELERIK REPORTING? Do you feel that it is fantastic? Or easy to use? Or better than Crystal Reports? Tell the world, and help fellow developers! Write a short review about Telerik Reporting and Telerik Report Designer in Visual Studio Gallery today!

0
Randy
Top achievements
Rank 1
answered on 06 Nov 2012, 07:17 PM
Thanks! The ToList() call in my query solved the problem.
Tags
General Discussions
Asked by
Randy
Top achievements
Rank 1
Answers by
Peter
Telerik team
Randy
Top achievements
Rank 1
Steve
Telerik team
Share this question
or