InvalidCastException when trying to stream a byte array, I think...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lamiumthetanker
    New Member
    • Nov 2011
    • 1

    #1

    InvalidCastException when trying to stream a byte array, I think...

    Hi guys, really stuck here, to be honest, I'm not really sure where I'm going wrong here. What I'm trying to do is to stream a PDF document to a browser by clicking a button. Below is a snippet of the code that I am using.

    I am reading the pdf correctly but for some reason it keeps thinking that my Byte is an Integer and keeps throwing an InvalidCastExce ption Error

    Here is the code snippet:

    Code:
    Const ChunkSize As Integer = 1024
    Dim buffer() As Byte = New Byte(ChunkSize) {}
    Dim binary(reader("document")) As Byte *** Exception is thrown here***
    Dim ms As New MemoryStream(binary)
    Dim SizeToWrite As Integer = ChunkSize
    Here is the error:

    Code:
    Server Error in '/' Application.
    Conversion from type 'Byte()' to type 'Integer' is not valid.
    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.InvalidCastException: Conversion from type 'Byte()' to type 'Integer' is not valid.
    
    Source Error:
    
    Line 40:             Const ChunkSize As Integer = 1024
    Line 41:             Dim buffer() As Byte = New Byte(ChunkSize) {}
    Line 42:             Dim binary(reader("document")) As Byte
    Line 43:             Dim ms As New MemoryStream(binary)
    Line 44:             Dim SizeToWrite As Integer = ChunkSize
    and here is the Stack Trace:

    Code:
    [InvalidCastException: Conversion from type 'Byte()' to type 'Integer' is not valid.]
       Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value) +987
       HardCopyOrderForm.Download.btnPDF_Click(Object sender, EventArgs e) in ***************************************************************************************************************   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Working...