Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I am getting this error Message "cannot redirect after http headers have been sent"
when I am calling response.redirect ("home.aspx").

this error occured after downloading file from gridview which is infragistic control.following code to downloading file..

so how can i redirect to the page. Its urgent..

C#
string fileName=this.Controller.Model.SessionDataContext.FileAttachmentRowData.PhysicalFileName;
            byte[] array = this.Controller.Model.SessionDataContext.FileAttachmentRowData.FileContents;
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
            Response.AddHeader("Content-Length", array.Length.ToString());
            Response.ContentType = "application/pdf";
            Response.OutputStream.Write(array, 0, array.Length);
            Response.Flush();
            Response.Clear();
Posted
Updated 27-Feb-20 19:46pm
v4

Hi,

You may have used Response.Redirect twice continuously which causes this error.
Refer to :
http://stackoverflow.com/questions/5646762/i-am-getting-exception-cannot-redirect-after-http-headers-have-been-sent-when[^]
 
Share this answer
 
Comments
Ajay ghosalkar 17-Apr-12 10:07am    
if removed response.flush then page will be redirected but i am not able to download file..page has to be redirected after downloading file. so what to do that..pls give me right solution
jjackie1 18-Sep-13 7:07am    
Have you got the answer for this probelm?? if so can you explain it here.
Hi, Just try the following code. Sometimes the request execute twise at same time with different header. Bellow code prevent the error and take first request.
if (!Response.IsRequestBeingRedirected)
{
Response.Redirect("http://www.yoururl.com");
}
Hope this will help other also
 
Share this answer
 
Comments
OriginalGriff 14-May-21 1:49am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 9 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more! (Particularly when he originally considered it "urgent"...)
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900