Response.close messes things up

Oct 29, 2011 at 7:20 PM

I have been implementing  a year end processing web page as part of our site and used the article, http://www.4guysfromrolla.com/articles/092910-1.aspx, as a guide. Everything works just fine for creating the zip file. Even being able to allow the user to save the zip to CD/DVD or anywhere else.

 My problem comes just after completing the download where I want to update a textbox and button text. They happen in code but the client’s page never gets refreshed showing the text.

 Can you help?

 Here is the ASPX source:

 <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/App_Masters/MasterPage.Master"

    CodeBehind="YearEnd.aspx.vb" Inherits="Community.YearEnd1" Async="true" %>
 
<%@ Register Assembly="TALogicWebControlLibrary" Namespace="TALogicWebControlLibrary.TALogicCustomWebControls"
    TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table id="tblYearEnd" style="color: navy; width: 600; text-align: center; padding: 5;
        border: thick ridge #f0f8ff; background-color: #f0f8ff;">
        <tr>
            <td style="background-image: url(../Backgrounds/TextRow.gif)">
                <span style="color: #f5f5f5; font-size: 14pt; text-decoration: underline"><strong>
                Community Year
                    End Processing</strong></span>
            </td>
            <td style="background-image: url(../Backgrounds/TextRow.gif)">
                <cc1:PopupBrowser ID="PopupBrowser1" runat="server" ImageUrl="../Images/Help.gif"
                    LinkUrl="~/Popups/YearEnd.htm" PopupLocation="TopLeft" 
                    ToolTip="Community backup help." />
            </td>
        </tr>
        <tr>
            <td colspan="2" style="white-space: nowrap" align="left">
                <p align="center">
                    Year End Processing performs the following tasks:</p>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left" width="600">
                <ol>
                    <li>Backup the community database to a dated backup.</li>
                    <li>Creates a Zip File of the current community data. This includes all ads, 
                        attachments, community documents, events, financials, images, imports, insurance 
                        documents, legal documents, letters, minutes,
                        newsletters, operational documents, policy documents, proxy documents, home page 
                        features, 
                        vendor, and video information as well as the previously created date backup of the&nbsp;database.</li>
                    <li>Deletes community data over 7 years old in accordence with IRS practices. This includes all events, financials, 
                        insurance documents,&nbsp; letters, minutes,
                        newsletters,&nbsp; proxy, vendor specific documents, and video information.</li>
                    <li>Updates the database to reflect the deleted old community data.</li>
                    <li>It is recommended that the created Zip archive be saved to a safe place or to a 
                        CD/DVD for safe keeping.</li>
                </ol>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                Once started, <span style="color: maroon; text-decoration: underline;">Do Not Turn Off</span> your computer!
                <br />
                Year End Processing will be completed successufully when download completes.
            </td>
        </tr>
        <tr>
            <td colspan="2">
                    <asp:Button runat="server" ID="BtnYearEnd" OnClientClick="this.disabled = true; this.value = 'Processing... Please Wait!';"
                        UseSubmitBehavior="false" Text="Start Year End Processing" CommandName="StartYearEnd" />
 
                    <br />
                    <asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        </table>
</asp:Content>

 Here is the code that is run in the button command (I marked the parts that don’t seem to fire):

     Private Sub BtnYearEnd_Command(sender As Object, e As System.Web.UI.WebControls.CommandEventArgsHandles BtnYearEnd.Command

        Select Case e.CommandName

            Case "StartYearEnd"

                Try

                    'Get the path to the Database Backup directory for this community

                    Dim bUri As String = GetBackupsRelativeUri(Session("Community_Id"))

                    Dim bMappedPath As String = Server.MapPath(bUri)

                     'Get the path to the Data directory for this community

                    Dim rUri As String = GetCommunityRelativeUri(Session("Community_Id"))

                    Dim sMappedPath As String = Server.MapPath(rUri)

                     'Get the path to the Common files directory

                    Dim cUri As String = GetCommonDataRelativeUri()

                    Dim cMappedPath As String = Server.MapPath(cUri)

                     'Get the path to the Management directory for this community

                    Dim thisAssoc As Association = New Association

                    Dim assocDetail As AssociationDetail = thisAssoc.GetAssociationDetail(Session("Community_Id"))

                    Dim mUri As String = GetMgmtRelativeUri(assocDetail.ManagementId)

                    Dim mMappedPath As String = Server.MapPath(mUri)

 #If DEBUG Then

                    If True Then '(for testing only - Does NOT create database backup)

#Else

                    'Do a dbBackup to be included in the archive

                    If BackupDatabase(bMappedPath, backupType.Dated) Then

#End If

                        Try

                            'Ok to archive now

                            Using zip As New ZipFile()

                                Response.Clear()

                                Response.BufferOutput = False

                                 Dim c As System.Web.HttpContext = System.Web.HttpContext.Current

                                Dim ReadmeText As String = String.Format("README.TXT" & vbCrLf & _

                                                     "This is a zip file that was dynamically generated at {0} " & _

                                                     "as part of the community year end processing for the '{1}' community." & vbCrLf & _

                                                     "It includes community data that was in affect when this archive was created." & vbCrLf & _

                                                     "It is further recommended that this archive be saved in a save place.", _

                                                     System.DateTime.Now.ToString(), _

                                                     assocDetail.AssocName)

                                 Dim archiveName As String = String.Format("archive-{0}.zip"DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))

                                Response.ContentType = "application/zip"

                                Response.AddHeader("content-disposition""filename=" + archiveName)

                                 'Add the Readme.txt file

                                zip.AddEntry("Readme.txt", ReadmeText, Encoding.Default)

                                 'Process all the Community Data directories & sub directories

                                ArchiveFiles(sMappedPath, zip)

                                 'Process all the Community Common directories & sub directories

                                ArchiveFiles(cMappedPath, zip)

                                 'Process all the Community Management directories & sub directories

                                ArchiveFiles(mMappedPath, zip)

                                 'Save the archive

                                zip.Save(Response.OutputStream)

                            End Using

                            Response.Close()

                             'Zip was created OK - Delete old records

                            DeleteOldFiles(sMappedPath, CInt(Session("Community_Id")))

 

                          ‘******************** This is the part that never gets back to the client ************************

                            'Completed Successfully

                            TextBox1.Text = "Year End Processing is done: " & DateTime.Now.ToLongTimeString()

                            TextBox1.Visible = True

 

                            sender.Enabled = False

                            sender.Text = "Completed Successfully!"

 

                          ‘‘********************** And this alert never fires ***************************

                            ScriptManager.RegisterStartupScript(BtnYearEnd, BtnYearEnd.GetType(), "script""alert('Year end processing has completed successfully.');"True)

 

                        Catch ex As Exception

                            'Something went wrong

                            sender.Enabled = True

                            sender.Text = "Start Year End Processing"

 

                            ScriptManager.RegisterStartupScript(BtnYearEnd, BtnYearEnd.GetType(), "script""alert('Year end processing has been aborted, please try again later.');"True)

                            AddErr(ex, Session("Community_Id"))

                        End Try

                    Else

                        'Database backup filed

                        sender.Enabled = True

                        sender.Text = "Start Year End Processing"

                        ScriptManager.RegisterStartupScript(BtnYearEnd, BtnYearEnd.GetType(), "script""alert('Database backup failed. Year end processing has been aborted, please try again later.');"True)

                    End If

                 Catch ex As Exception

                    sender.Enabled = True

                    sender.Text = "Start Year End Processing"

                    ScriptManager.RegisterStartupScript(BtnYearEnd, BtnYearEnd.GetType(), "script""alert('Error with year end processing please try again.');"True)

                    'Note the exception

                    AddErr(ex, Session("Community_Id"))

                Finally

                 End Try

         End Select

    End Sub

Coordinator
Nov 7, 2011 at 4:43 PM

Try Response.End in place of Response.Close

There has been some discussion of these things over the years.