Server cannot append header after HTTP headers have been sent. error

hi,

i am usig aspose.cells to create a excel file and iam trying to allow the user to download that file. while doing that its giving the error

Server cannot append header after HTTP headers have been sent.

its working fine when separate the code and paste in two buton events.

my code is:

Dim workbook As Workbook = New Workbook()

workbook.Worksheets.Add()

Dim worksheet As Worksheet = workbook.Worksheets(0)

Dim RS As SqlCommand = New SqlCommand()

Dim strSQL As String

Dim MyConn As SqlConnection = New SqlConnection()

MyConn.ConnectionString = ConfigurationManager.ConnectionStrings("Small_ScanConnectionString").ToString

If GridView1.Visible = True Then

strSQL = "exec usp_rpt_SubmissionReportOne '" & User.Identity.Name.ToString & "', '" & textBoxStartDate.Text & "','" & textBoxEndDate.Text & "'"

RS.CommandText = strSQL

RS.Connection = MyConn

RS.CommandType = Data.CommandType.Text

MyConn.Open()

Dim newDataSet As DataSet = New DataSet()

Dim newDataTable As DataTable = New DataTable()

Dim newDataAdapter As SqlDataAdapter = New SqlDataAdapter(RS)

newDataAdapter.Fill(newDataTable)

worksheet.Cells.ImportDataTable(newDataTable, True, "A2")

workbook.Save(System.Configuration.ConfigurationManager.AppSettings("generatedReportPath") & Session.SessionID & ".xls")

MyConn.Close()

End If

If GridView2.Visible = True Then

strSQL = "exec usp_rpt_SubmissionReportTwo '" & User.Identity.Name.ToString & "', '" & textBoxStartDate.Text & "','" & textBoxEndDate.Text & "'"

RS.CommandText = strSQL

RS.Connection = MyConn

RS.CommandType = Data.CommandType.Text

MyConn.Open()

Dim newDataSet As DataSet = New DataSet()

Dim newDataTable As DataTable = New DataTable()

Dim newDataAdapter As SqlDataAdapter = New SqlDataAdapter(RS)

newDataAdapter.Fill(newDataTable)

worksheet.Cells.ImportDataTable(newDataTable, True, "A2")

workbook.Save(System.Configuration.ConfigurationManager.AppSettings("generatedReportPath") & Session.SessionID & ".xls")

MyConn.Close()

End If

System.Threading.Thread.Sleep(3000)

Dim fileName As String = Session.SessionID & ".xls"

Response.Clear()

Response.ContentType = "application/ms-excel"

Response.AddHeader("content-disposition", "attachment; filename=" & fileName) 'error is coming here

Response.TransmitFile("Reports\" & fileName)

Response.Flush()

thanks

Hi,

Thanks for considering Aspose.

Well, it looks strange as your code works when separated. But I think you may try to change your code and use the Save method of the Workbook. e.g., public void Save(string,FileFormatType,SaveType,HttpResponse) and adjust your code accordingly. I think alternatively you may also try to comment out the line of code to see if it works fine.... i.e. System.Threading.Thread.Sleep(3000) as this line might be responsible for the error when the code is as a whole.

And could you tell me how do you separate your code.

Thank you.

And you can check our demos source code for how to stream a file to client browser.