Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am Exporting Huge data into excel ,getting white pages in browser, can u guide me or send snippets




protected void Button1_Click(object sender, EventArgs e)
{

ExportToExcel((DataTable)ViewState["gvMobile"]);
}
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "DownloadMobileNoExcel.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();

//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
private static DataTable SampleData()
{
//SqlConnection con = new SqlConnection("Data Source=GSPSDELL201;Initial Catalog=Test;User ID=sa;Password=sa");
//con.Open();
DataTable sampleDataTable = new DataTable();

sampleDataTable.Columns.Add("FirstColumn", typeof(string));
sampleDataTable.Columns.Add("SecondColumn", typeof(string));
DataRow sampleDataRow;
for (int i = 1; i <=200000; i++)
{

sampleDataRow = sampleDataTable.NewRow();
sampleDataRow["FirstColumn"] = "Cell1: " + i.ToString(CultureInfo.CurrentCulture);
sampleDataRow["SecondColumn"] = "Cell2: " + i.ToString(CultureInfo.CurrentCulture);
sampleDataTable.Rows.Add(sampleDataRow);
}

return sampleDataTable;
}
Posted
Updated 27-Jun-14 20:46pm
v2

1 solution

1.First problem in your code is that you are using data from database without to use pagination, and this for huge amount of data creates big problem in web application.

2.The second problem, that is also related with the first one, is that you are caching all datatable with the huge amount of data inside into the view state and in this all huge amount of data are sending between the postbacks (from browser to the web server). You don't need that because you have all data into the database and you can read them directly from your code (that is running on the web server).

3.To solve all your problem you should implement pagination in your grid view and at the export to excel time to read the huge amount of data directly from the database in your code behind.

You could find details about grid view and pagination in my next article (and the provided source code): Advanced ASPX GridView Pagination and Data Entities[^]
 
Share this answer
 
Comments
kingsa 28-Jun-14 6:14am    
Thank u , with Great solution ,
i changed as per your sujjesstions its working fine when i deploy application in server i am getting same problem and what the reason getting white pages

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900