Hi All,
When am Exporting Huge data From datagridview to Excel am getting this kind of Problem.Please Help me..
ERROR:
Connection Interrupted
The connection to the server was reset while the page was loading.
The network link was interrupted while negotiating a connection. Please try again.
Try Again
same code will work to export the small amount of data..
protected void btnExcel_Click(object sender, ImageClickEventArgs e) { string fileName = "CurrentStockLedger.xls"; string Extension = ".xls"; if (Extension == ".xls") { PrepareControlForExport(GridView1); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public); HttpContext.Current.Response.ContentType = "application/ms-excel"; try { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // Create a form to contain the grid System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table(); table.GridLines = GridView1.GridLines; // add the header row to the table if (GridView1.HeaderRow != null) { PrepareControlForExport(GridView1.HeaderRow); table.Rows.Add(GridView1.HeaderRow); } // add each of the data rows to the table foreach (GridViewRow row in GridView1.Rows) { PrepareControlForExport(row); table.Rows.Add(row); } // add the footer row to the table if (GridView1.FooterRow != null) { PrepareControlForExport(GridView1.FooterRow); table.Rows.Add(GridView1.FooterRow); } // render the table into the htmlwriter GridView1.GridLines = GridLines.Both; table.RenderControl(htw); // render the htmlwriter into the response HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } } } catch (HttpException ex) { throw ex; } } } private static void PrepareControlForExport(Control control) { for (int i = 0; i < control.Controls.Count; i++) { Control current = control.Controls[i]; if (current is LinkButton) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); } else if (current is ImageButton) { control.Controls.Remove(current); //control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); } else if (current is HyperLink) { control.Controls.Remove(current); //control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); } else if (current is DropDownList) { control.Controls.Remove(current); //control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); } else if (current is CheckBox) { control.Controls.Remove(current); //control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); } else if (current is HiddenField) { control.Controls.Remove(current); //control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); } if (current.HasControls()) { PrepareControlForExport(current); } } }