0

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.

mitaka
  • 2,159
  • 1
  • 30
  • 30
  • I was watching the Windows Task Manager and it did not exceeded 2GB of RAM "PF Usage". The Server has 4GB available, but I guess only 1GB is available per application. – mitaka May 24 '13 at 16:14
  • 1
    jqGrid have **no** `"Export to Excel"` button. Do you use some commercial product based on jqGrid? Do you load all 20000 rows in the grid per `loadonce: true`? Is it not better to export to Excel **on the server side**? See [the answer](http://stackoverflow.com/a/9349688/315935) for the code example. Demo project from the answer can be downloaded [here](http://www.ok-soft-gmbh.com/jqGrid/jqGridExportToExcel.zip). – Oleg May 24 '13 at 16:28
  • Actually it is the System.Web.UI.WebControls.GridView.DataBind() call that throws the Out of Memory Exception on the server side, not the jqGrid. – mitaka May 24 '13 at 18:57
  • 1
    Break down 1 large data into smaller chucks and send it out little by little to GridView (Smaller memory footprint). But w/ jqGrid or GridView, when exporting to Excel, the problem is the web browser doesn't do HTTP header, meaning you have to do it from the webserver. (one way is to sent back to the server but that is not ideal due to overhead). – fletchsod May 24 '13 at 19:11
  • Thanks, I was reading the MSDN [link](http://msdn.microsoft.com/en-us/library/9w766t6y(v=vs.80).aspx). Looks I have to buffer the data. – mitaka May 24 '13 at 19:31

0 Answers0