title: How to render Active Reports WebViewer in ASP.NET MVC
body: <p>I asked this question in the DataDynamics forum earlier today.  I thought that maybe I'd get some sort of response here at SO.</p>
  
  <p>I am trying to get the WebViewer up and running in my ASP.NET MVC application. 
  I am attempting to render the webviewer in the controller (webViewer.RenderControl(htmlTextWriter) and then put the results into ViewData and display the report in my view. I dont' even know if this is the correct way to go about this. Any help would be greatly appreciated. </p>
  
  <p><strong>Controller code:</strong></p>
  
  <pre><code>    public ActionResult Display()
      {
  
          CurrentReport = new DetailedReport { ReportData = new DetailedData() { Repository = _repository } };
  
          var webViewer = new WebViewer();
          CurrentReport.Run();
          webViewer.ID = "WebViewer1";
          webViewer.Visible = true;
          webViewer.ViewerType = ViewerType.HtmlViewer;
          webViewer.Width = Unit.Percentage(100);
          webViewer.Report = CurrentReport;
  
  
          var stringWriter = new StringWriter();
          var htmlTextWriter = new HtmlTextWriter(stringWriter);
          webViewer.RenderBeginTag(htmlTextWriter);
          webViewer.RenderControl(htmlTextWriter);
          webViewer.RenderEndTag(htmlTextWriter);
  
          ViewData["WebViewer"] = stringWriter.ToString();
  
          return View();
      }
  </code></pre>
  
  <p><strong>Display.aspx code:</strong></p>
  
  <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage" %&gt;
  &lt;%@ Register assembly="ActiveReports.Web, Version=5.2.1013.2, Culture=neutral, PublicKeyToken=cc4967777c49a3ff" namespace="DataDynamics.ActiveReports.Web" tagprefix="ActiveReportsWeb" %&gt;
  &lt;%@ Import Namespace="xxxx.Core"%&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="ClientAdminContent" runat="server"&gt;
      &lt;%=ViewData["WebViewer"] %&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <p>Object reference not set to an instance of an object. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.</p>
  
  <p>Source Error: </p>
  
  <p>Line 60:            var htmlTextWriter = new HtmlTextWriter(stringWriter);
  Line 61:            webViewer.RenderBeginTag(htmlTextWriter);
  Line 62:            webViewer.RenderControl(htmlTextWriter);
  Line 63:            webViewer.RenderEndTag(htmlTextWriter);
  Line 64: </p>
  
  <p>Source File: C:\Projects\xxxx\xxxx\app\xxxx.Web.Controllers\ReportsController.cs    Line: 62 </p>
  
  <p>****Update:****</p>
  
  <p>Based on the answer by scott (thank you) my controller now looks like this:</p>
  
  <pre><code>    public ActionResult Display()
      {
          ViewData["Report"] = new DetailedReport { ReportData = new DetailedReport { ReportData = new DetailedData() { Repository = _repository } };
          return View();
      }
  </code></pre>
  
  <p>And my view looks like this: (I have no code behind files for my views).</p>
  
  <pre><code>&lt;%
      var report = (ActiveReport3) ViewData["Report"];
      report.Run();
      WebViewer1.Report = report;
  %&gt;
  &lt;ActiveReportsWeb:WebViewer ID="WebViewer1" runat="server" Height="100%" Width="100%" ViewerType="AcrobatReader" /&gt;
  </code></pre>
  
  <p>I watch it go through debugger, and it seems to correctly step through the Details section, putting values into my fields.  But after all is done, I get the message <strong>"No Report Specified."</strong>  I'm hoping that I really don't have to use a codebehind file on my view because I'm not using them anywhere else.  I have also debugged to verify that report.Document.Pages.Count > 0.  I have put the code block both above and below the WebViewer control (don't think that really matters).  Any additional thoughts?</p>
  
  <p>****Update #2:****</p>
  
  <p>I ended up using the answer found here: <a href="https://stackoverflow.com/questions/566902">Alternative to using the OnLoad event in an ASP.Net MVC View?</a> in combination with scott's excellent answer below.  It was a timing thing with generating and binding the report to the control.  So my View looks like this in the end... (where Model.Report is an ActiveReport3)</p>
  
  <pre><code>&lt;script runat="server"&gt;
      private void Page_Load(object sender, EventArgs e)
      {
          var report = Model.Report;
          report.Run();
          WebViewer1.Report = report;
      }
  &lt;/script&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="ClientAdminContent" runat="server"&gt;
      &lt;ActiveReportsWeb:WebViewer ID="WebViewer1" runat="server" Height="100%" Width="100%" ViewerType="AcrobatReader" /&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Thanks for everyone's help!</p>