简体   繁体   中英

System.OutOfMemoryException on gridview

I have a problem with my application.

First of all, sorry about my English, I'm from Chile and I'm still learning.

I have a method that gives an Excel with information from a db. Clients don't need to see the report on screen, they just need to download it from app.

This method calls a stored procedure, which gives too much data (about 600 columns and 20000 rows), so, when table is assigned to a GridView, on DataBind(), throws a System.OutOfMemoryException .

Is there any other way to load the GridView with data? I can't filter in query, or simplify it, because client needs every date from the report. I'm using Visual Studio 2010, Framework 4.0, SQL Server 2008.

This is the method:

private void getsReport()
{
    string reportName = "Globarl_Report_" +     User.Identity.Name + "_" + DateTime.Now.ToString("yyyyMMdd")+".xls";
    Database db = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["SNAT"].ToString());
    DBCommandWrapper com = db.GetSqlStringCommandWrapper("usp_con_global_report);// sp that give report.

    com.CommandTimeout = 360;

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Page pagina = new Page();
    HtmlForm form = new HtmlForm();
    DataTable tabla = db.ExecuteDataSet(com).Tables[0]; ;
    GridView dg = new GridView();

    dg.EnableViewState = false;
    dg.DataSource = tabla;
    dg.DataBind();//THIS LINES THROWS EXCEPTION!!
    pagina.EnableEventValidation = false;
    pagina.DesignerInitialize();
    pagina.Controls.Add(form);
    form.Controls.Add(dg);
    pagina.RenderControl(htw);
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + nombreArchivo);
    Response.Charset = "UTF-8";
    Response.ContentEncoding = Encoding.Default;
    Response.Write(sb.ToString());
    Response.End();
}

thanks for your help!!

You should add a pager to your gridview.

Look here : Paging and Sorting the GridView's Data

Are you loading all data to grig to make it possible to export to Excel only?

In such case you can create csv file on server (by schedule or by client request dynamically) and put a link on page to download it.

Later it can be opened with Excel (or any other spreadsheet processor)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM