ASP.NET Error
I have written this piece of code to export grid data to excel. And I get this error on debugging.
Know the answer? Post it. Somebody with the same question will find it here.





Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 14, 2013, 10:59 PM EDT
i checked your code just check you forgot to add this in both BSP and APP:
for Bsp:
for APP:
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 12, 2013, 2:50 AM EDT
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
if not just copy and paste it within your code.
In your code:
protected void Button2_Click(object sender, ImageClickEventArgs e) // Wrong
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
just change it to this:
protected void Button2_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
if any problem reply
Vignesh KumarRank #1,579 · 1K reputationPosted Jul 12, 2013, 3:04 AM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 12, 2013, 3:05 AM EDT
Vignesh KumarRank #1,579 · 1K reputationPosted Jul 12, 2013, 3:14 AM EDT
Here are attached codes.
Ravi ShekharRank #673 · 3.6K reputationPosted Jul 12, 2013, 3:19 AM EDT
I think you should export data from your datatable. this is a good practice. check below code...
Vignesh KumarRank #1,579 · 1K reputationPosted Jul 12, 2013, 3:25 AM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 12, 2013, 3:28 AM EDT
In exel you are getting data or not.
Vignesh KumarRank #1,579 · 1K reputationPosted Jul 12, 2013, 3:31 AM EDT
ERROR
The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?)
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 12, 2013, 3:51 AM EDT
I saw your codes in that nested grid . may be due to that you are not getting actual result. so put your grid within a div and export it.
follow this link for do that:
http://nice-tutorials.blogspot.in/2011/01/export-dynamic-data-from-div-to-excel.html
any problem reply
Vignesh KumarRank #1,579 · 1K reputationPosted Jul 12, 2013, 3:57 AM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 12, 2013, 3:58 AM EDT
Pankaj PandeyRank #642 · 3.7K reputationPosted Jul 12, 2013, 4:02 AM EDT
using System.Data;
and try again with Sanjeeb code.
Vignesh KumarRank #1,579 · 1K reputationPosted Jul 12, 2013, 4:04 AM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 12, 2013, 4:10 AM EDT
i attached the code in txt using div and jquery. just check that.
1. add the jquer library.
2.then check the code carefully what i changed and add . in aspx and cs.
Vignesh KumarRank #1,579 · 1K reputationPosted Jul 12, 2013, 4:25 AM EDT
protected void Button2_Click(object sender, EventArgs e)
{
string html = HdnValue.Value;
ExportToExcel(ref html, "MyReport");
}
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 12, 2013, 4:31 AM EDT
just add this line after the div end and before the content at last line of aspx.