Wednesday, September 01, 2010

Infinite "Loading" Loop Message When Setting Parameters of an MS Report in ASP.NET 4.0 / 2010

I encountered a problem when setting the parameters of a Microsoft Report object (i.e., an RDLC file created in VS.NET 2010) and rendering it via a ReportViewer control in an ASP.NET 4.0 web application: When loading the report, it infinitely displayed the 'Loading' box, never actually rendering the actual report. This occurs because the parameters set are posted back to the web server, to the Reserved.ReportViewerWebControl.axd web resource, and if this is done on every page load event, the parameters are set infinitely (on each page load), with the web form never having a chance to complete. Therefore, the solution is to ensure the parameters are not set in a post-back event. Here's an example:

protected void Page_Load(object sender, EventArgs e) { try { if (!Page.IsPostBack) { if (!String.IsNullOrEmpty(Request.QueryString["ID"])) { var story = Service.StoryService.DefaultInstance.GetStory(int.Parse(Request.QueryString["ID"]), Domain.Story.GetStoriesCaller.AdministrationArea); if (story != null) { this.reportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/Story.rdlc"); var parameters = new List<Microsoft.Reporting.WebForms.ReportParameter>(); parameters.Add(new Microsoft.Reporting.WebForms.ReportParameter("Title", story.Title, false)); parameters.Add(new Microsoft.Reporting.WebForms.ReportParameter("Body", story.Title, false)); this.reportViewer1.LocalReport.SetParameters(parameters); } else throw new NullReferenceException(String.Format("No story was found matching story ID #{0}.", Request.QueryString["ID"])); } else throw new NullReferenceException("No story ID string string was provided."); } } catch (Exception err) { this.OnError(err); DotNetFun.Sites.Service.ExceptionLogService.DefaultInstance.Add( Error: err, PageURL: Request.Url.AbsoluteUri, UserID: (CurrentUser.IsLoggedIn ? CurrentUser.Info.UserID : (int?)null)); } }