I get an error (Server cannot set content type after HTTP headers have been sent.) on the following code (ContentType
line). What should I change?
Do you need to set the content type? This solution should not be overlooked, because you may find your code to work perfectly without the need to explicitly define the content type for the type of response you are sending. So removing the following line should make the error go away, and may very well do so without introducing new problems (of course you'll want to test this against your scenario):
response.ContentType = "text/plain";
This solution worked for me, add this line of code on the beginning of the method,
Server.ClearError();
I had a very similar issue to this on a webform. I solved this issue by adding the following code to my button in the code behind:
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnPrint);
I had the same problem and the following solution solved the problem,
run a response.ClearHeaders();
before run response.AddHeader(
Have a look at HttpResponse.BufferOutput Property
http://msdn.microsoft.com/en-us/library/33cy25ty%28v=vs.100%29.aspx
Try setting response.BufferOutput = true;
. Do this immediately after setting the response
variable.