This is the code :
TextWriter writer = null;
HttpResponse response = new HttpResponse(writer);
response.ClearContent();
response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
// response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
HtmlForm htmlForm = new HtmlForm();
GridView1.Parent.Controls.Add(htmlForm);
htmlForm.Attributes["runat"] = "server";
htmlForm.Controls.Add(GridView1);
htmlForm.RenderControl(htmlTextWriter);
response.Write(stringWriter.ToString());
If I run this code, it throws an NullReferenceException. I used this codes in a Business Logic Layer