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:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
gvDataBind()
gvDataBind1()
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"
Download1()
Case "divExport1"
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)
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)
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