why ZlibException throws when export data to files using DevExpress?
<p>I have some sql tables with dynamic columns which i want to export them to <code>xls</code> or <code>csv</code> file using <a href="https://www.devexpress.com/" rel="nofollow noreferrer">DevExpress</a> <code>ASPxGridViewExporter</code> and <code>ASPxGridView</code> when user clicks <code>Get Data</code> button in my <code>Report.aspx</code> page.</p>

<ul>
<li>Tables are made correctly with true data</li>
<li>Page loads successfully and shows data truly</li>
</ul>

<p><strong>My Problem</strong> </p>

<p>When user clicks the button, it downloads a corrupted file with following content, </p>

<p>I searched <a href="https://google.com" rel="nofollow noreferrer"><strong>google</strong></a>, <a href="https://stackoverflow.com/"><strong>stackoverflow</strong></a>, <a href="https://support.microsoft.com/" rel="nofollow noreferrer"><strong>microsoft support</strong></a>, <a href="https://www.devexpress.com/Support/" rel="nofollow noreferrer"><strong>devexpress support</strong></a> and so many sites, but there was no answer!</p>

<p>any help will be appreciated. </p>

<pre><code>Server Error in '/' Application.        


The stream state of the underlying compression routine is inconsistent.     

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.IO.Compression.ZLibException: The stream state of the underlying compression routine is inconsistent.     

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:        

[ZLibException: The stream state of the underlying compression routine is inconsistent.]        
   System.IO.Compression.DeflaterZLib.Deflate(FlushCode flushCode) +196     
   System.IO.Compression.DeflaterZLib.ReadDeflateOutput(Byte[] outputBuffer, FlushCode flushCode, Int32&amp; bytesRead) +185        
   System.IO.Compression.DeflaterZLib.System.IO.Compression.IDeflater.GetDeflateOutput(Byte[] outputBuffer) +44     
   System.IO.Compression.DeflateStream.WriteDeflaterOutput(Boolean isAsync) +58     
   System.IO.Compression.DeflateStream.Write(Byte[] array, Int32 offset, Int32 count) +76       
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +578        
   System.Web.HttpResponse.UpdateNativeResponse(Boolean sendHeaders) +1272      
   System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async) +191        
   System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size) +88     
   System.IO.Compression.DeflateStream.PurgeBuffers(Boolean disposing) +142     
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +40       
   System.IO.Stream.Close() +26     
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +42      
   System.IO.Stream.Close() +26     
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +767        
   System.Web.HttpResponse.FilterOutput() +127      
   System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +62       
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +98     

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0         
</code></pre>

<p><strong>My Questions</strong></p>

<ul>
<li>Why <code>ZLibException</code> thorws?</li>
</ul>

<p>Here is my codes:</p>

<p><strong>ASP</strong></p>

<pre><code>&lt;%@ Register Assembly="DevExpress.Web.v16.1" Namespace="DevExpress.Web"
    TagPrefix="dx" %&gt;
&lt;%@ Register Assembly="DevExpress.XtraPrinting.v16.1" Namespace="DevExpress.Web"
    TagPrefix="dx" %&gt;

&lt;%@ Register  Assembly="DevExpress.Printing.v16.1.Core, Version=16.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Printing"
    TagPrefix="dx" %&gt;

&lt;asp:Content ID="Content2" ContentPlaceHolderID="C" runat="Server"&gt;
    &lt;div id="divMain" class="main" style="padding: 10px"&gt;
        &lt;asp:ObjectDataSource ID="ods" runat="server" TypeName="MySite.Control.CustomTable" SelectMethod="ReadTable"&gt;
            &lt;SelectParameters&gt;
                &lt;asp:Parameter Type="Int64" Name="ID" /&gt;
            &lt;/SelectParameters&gt;
        &lt;/asp:ObjectDataSource&gt;

        &lt;dx:ASPxGridView ID="grid" runat="server" ClientInstanceName="grid" AutoGenerateColumns="false"
            KeyFieldName="ID" Width="100%"&gt;
            &lt;Columns&gt;
                &lt;!-- Columns Will Be Generated Dynamically --&gt;
            &lt;/Columns&gt;
            &lt;SettingsCustomizationWindow Enabled="True" /&gt;
            &lt;SettingsBehavior ColumnResizeMode="Control" AllowDragDrop="true" AllowFocusedRow="true" /&gt;
            &lt;Settings ShowGroupPanel="True" ShowColumnHeaders="true" ShowFilterBar="Visible"
                ShowFooter="true" ShowGroupFooter="VisibleIfExpanded" ShowGroupButtons="true"
                ShowFilterRow="true" ShowFilterRowMenu="true" /&gt;
            &lt;SettingsLoadingPanel Mode="ShowOnStatusBar" /&gt;
            &lt;GroupSummary&gt;
            &lt;/GroupSummary&gt;
        &lt;/dx:ASPxGridView&gt;

        &lt;!-- ASPxGridViewExporter --&gt;
        &lt;dx:ASPxGridViewExporter ID="exporter" runat="server" GridViewID="grid"&gt;
        &lt;/dx:ASPxGridViewExporter&gt;


        &lt;div style="direction: rtl"&gt;
            &lt;asp:DropDownList ID="exportType" runat="server"&gt;
                &lt;asp:ListItem Text="XLS" Value="XLS" /&gt;
                &lt;asp:ListItem Text="CSV" Value="CSV" /&gt;
            &lt;/asp:DropDownList&gt;

            &lt;asp:Button ID="btnExport" runat="server" Text="Get Data" OnClick="BtnExportClick" /&gt;
        &lt;/div&gt;

    &lt;/div&gt;


&lt;/asp:Content&gt;
</code></pre>

<p><strong>C#</strong></p>

<pre><code>protected void BtnExportClick(object sender, EventArgs e)
{
    grid.SettingsDetail.ExportMode = GridViewDetailExportMode.All;
    switch (exportType.SelectedValue)
    {
        case "XLS":
            exporter.WriteXlsToResponse();
            break;
        case "CSV":
            exporter.WriteCsvToResponse();
            break;
        default:
            return;
    }
}
</code></pre>