GridView.DataBind() Out Of Memory Exception on Exporting Large Data Set to Excel

1.8k Views Asked by At

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)

I am getting the {"Exception of type 'System.OutOfMemoryException' was thrown."} on the DataBind() call:

    public void ExportToExcel()
    {

        if (Session["query"] == null || Session["fieldNameAsDef"] == null)
        {
            return;
        }
        var grid = new GridView();
        List<string> fieldNameAsDef = (List<string>)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();
    }

Is that limitation of the System.Web.UI.WebControls.GridView? Or it is IIS 6 and MVC2 issue.

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.

0

There are 0 best solutions below