views:

73

answers:

1

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

+1  A: 

You're getting a runtime error when you hit the response.Write line because you are passing null in as your stream for the response:

TextWriter writer = null;
HttpResponse response = new HttpResponse(writer);

The code you posted is very sloppy. I'd suggest you rewrite the method from scratch. If you don't you'll probably encounter other runtime errors once you get past this one.

Michael Meadows