Click here to Skip to main content
15,615,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have few gridviews with div for each of the grid to maintain the format of the excel files in my page, I have to loop through the number of div and export the entire div into a seperate excel file based on some condition.

If I export only one div to an excel file it works fine but when I try to loop and export all the div then it is generating an exception.

exception:
Server Error in '/' Application.
Session state has created a session id, but cannot save it because the response was already flushed by the application.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Session state has created a session id, but cannot save it because the response was already flushed by the application.
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): Session state has created a session id, but cannot save it because the response was already flushed by the application.]
System.Web.SessionState.SessionIDManager.SaveSessionID(HttpContext context, String id, Boolean& redirected, Boolean& cookieAdded) +3212120
System.Web.SessionState.SessionStateModule.CreateSessionId() +54
System.Web.SessionState.SessionStateModule.DelayedGetSessionId() +80
System.Web.SessionState.SessionStateModule.ReleaseStateGetSessionID() +19
System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +545
System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs) +9818638
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69



Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.79.0



Pls somebody help.
Thanks in advance

What I have tried:

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        gvDataBind() 'Binding data for 1st grid
        gvDataBind1() 'Binding data for 2nd grid
        download()

    End Sub

Private Sub download()
        Response.Clear()
        Response.Buffer = True
        For Each objControl As Control In Me.Controls
            If TypeOf (objControl) Is HtmlGenericControl Then
                Dim cntrl As String = objControl.ClientID

                Select Case cntrl
                    Case "divExport" '1st div that has to be exported
                        Download1()
                    Case "divExport1"  '2nd div that has to be exported
                        Download2()
                End Select
            End If
        Next
        Response.Flush()
        Response.[End]()
    End Sub
Private Sub Download1()
        Try
            tddatereport.InnerText = "Date" + ":" + DateTime.Now.AddDays(-1).Date().ToShortDateString()
            Response.AddHeader("content-disposition", "attachment; filename= Report1 For " + DateTime.Now.AddDays(-1).ToShortDateString() + ".xls")
            Response.Charset = ""
            Response.ContentType = "application/vnd.ms-excel"
            Using sw As New StringWriter()
                Dim hw As New HtmlTextWriter(sw)
                gv1.AllowPaging = False
                gv1.AllowSorting = False
                For i As Integer = 0 To gv1.Columns.Count - 1
                    If gv1.Columns(i).HeaderText = "" Then
                        gv1.Columns.RemoveAt(i)
                    End If
                Next
                divExport.RenderControl(hw)
                'style to format numbers to string
                Dim style As String = "<style> .textmode { } </style>"
                Response.Write(style)
                Response.Output.Write(sw.ToString())
            End Using

        Catch lException As System.Threading.ThreadAbortException
            Throw lException
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Private Sub Download2()
        Try
            tddatereport1.InnerText = "Date" + ":" + DateTime.Now.AddDays(-1).Date().ToShortDateString()
            Response.AddHeader("content-disposition", "attachment; filename= Report2 For " + DateTime.Now.AddDays(-1).ToShortDateString() + ".xls")
            Response.Charset = ""
            Response.ContentType = "application/vnd.ms-excel"
            Using sw As New StringWriter()
                Dim hw As New HtmlTextWriter(sw)
                gv2.AllowPaging = False
                gv2.AllowSorting = False
                For i As Integer = 0 To gv2.Columns.Count - 1
                    If gv2.Columns(i).HeaderText = "" Then
                        gv2.Columns.RemoveAt(i)
                    End If
                Next
                divExport1.RenderControl(hw)
                'style to format numbers to string
                Dim style As String = "<style> .textmode { } </style>"
                Response.Write(style)
                Response.Output.Write(sw.ToString())
            End Using

        Catch lException As System.Threading.ThreadAbortException
            Throw lException
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
Posted
Updated 17-Mar-21 3:35am
v2
Comments
Richard Deeming 31-Mar-16 8:18am    
You don't seem to understand how HTTP works. One request generates one response. That response contains a single HTML document or a single file. You cannot send multiple files in a single response, unless you combine them into a single container file such as a Zip file.

Also, HTML is not an Excel file. Whilst Excel will do its best to import an HTML file, for the best results you should be generating and sending a real Excel file, using something like EPPlus[^] or ClosedXML[^].

1 solution

Hi,

For one HTTP request there is only one response. So you must try to create excel files on server side, add those files in zip folder then download that folder.
 
Share this answer
 
v2

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