title: GridView.DataBind() Out Of Memory Exception on Exporting Large Data Set to Excel
body: <p>I have the "Export to Excel" button added to my jqGrid. It works fine until I linked the jqGrid to a large GridView with 20,000 records each having 200 fields (columns)</p>
  
  <p>I am getting the {"Exception of type 'System.OutOfMemoryException' was thrown."} on the DataBind() call:</p>
  
  <pre><code>    public void ExportToExcel()
      {
  
          if (Session["query"] == null || Session["fieldNameAsDef"] == null)
          {
              return;
          }
          var grid = new GridView();
          List&lt;string&gt; fieldNameAsDef = (List&lt;string&gt;)Session["fieldNameAsDef"];
          grid.DataSource = ((IQueryable)Session["query"]).Select("new (" + string.Join(",", fieldNameAsDef.ToArray()) + ")");
          grid.DataBind();
  
          Response.ClearContent();
          Response.AddHeader("content-disposition", "attachment; filename=EDGE_ExcelFile.xls");
          Response.ContentType = "application/excel";
          StringWriter sw = new StringWriter();
          HtmlTextWriter htw = new HtmlTextWriter(sw);
          grid.RenderControl(htw);
          Response.Write(sw.ToString());
          Response.End();
      }
  </code></pre>
  
  <p>Is that limitation of the System.Web.UI.WebControls.GridView?
  Or it is IIS 6 and MVC2 issue.</p>
  
  <p>The jqGrid and its GridView as DataSource is a part of older MVC 2 application running on Windows 2003 SP2 Server with IIS 6  and 4GB of RAM.</p>