0

I have a button on asp.net website that downloads the Excel file when I put certain values it points this error.

My code behind is as follows

 Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click
    Response.Clear()
    Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.xls"
    Dim stringWrite As New System.IO.StringWriter()
    Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
    GridView6.RenderControl(htmlWrite)
    Response.Write(stringWrite.ToString())
    Response.[End]()
End Sub

The button in aspx is as follows:

<asp:Button ID="Button4" runat="server" Text="Extract RTD Raw Data" nclick="Button4_Click" 
                 BackColor="#FF9966"  CssClass="myButton" Font-Bold="True" 
                 Font-Italic="True" />

I am getting this stack trace.

[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
System.Web.Util.HttpEncoder.UrlDecode(Byte[] bytes, Int32 offset, Int32 count, Encoding encoding) +76
System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) +164
System.Web.HttpRequest.FillInFormCollection() +77 [HttpException (0x80004005): The URL-encoded form data is not valid.]
System.Web.HttpRequest.FillInFormCollection() +130
System.Web.HttpRequest.EnsureForm() +69
System.Web.HttpRequest.get_Form() +13
System.Web.HttpRequest.get_HasForm() +9800635
System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) +95 System.Web.UI.Page.DeterminePostBackMode() +69 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +220

Snb93
  • 41
  • 2
  • 11
  • 1
    Start using a specialized library for creating Excel files, like [EPPlus](http://stackoverflow.com/documentation/epplus/drafts/98280) for example. All you are doing now is creating a HTML page with an .xls extension. – VDWWD Jul 27 '17 at 17:58
  • Is the datasource of `GridView6` especially large when you get this error? If so, then you will probably need to stream this to the client in chunks, rather than trying to copy the whole buffer (as it looks like you're legitimately hitting the process memory limit). See here for some options: https://stackoverflow.com/q/608480/861565 – Josh Darnell Jul 27 '17 at 19:48
  • @jadarnel Thanks for the advice I'll try going through the options – Snb93 Jul 27 '17 at 20:12
  • Solved the error by inserting `` in the Web.config file. Thanks for the guidance and help. – Snb93 Jul 28 '17 at 18:12

1 Answers1

0

After inserting the following tag <machineKey validationKey="929DF4BCF13739428C8C765842BD122C653C28F688D39‌​9F216942E5438AA880B4‌​A793C0772A51660FDA9A‌​69B079AB3B0ADFCFC149‌​41F5BBD673BC72E9804D‌​F44" decryptionKey="0B4443E8C132884733E8B26587FFCE50562CF8CF9980B‌​AD4813A95D54A7A369B" validation="SHA1" decryption="AES" /> in the Web.config file there is no error.

Snb93
  • 41
  • 2
  • 11