473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 InvalidCastException Error

Here is the code snippet:

Expand|Select|Wrap|Line Numbers
  1. Const ChunkSize As Integer = 1024
  2. Dim buffer() As Byte = New Byte(ChunkSize) {}
  3. Dim binary(reader("document")) As Byte *** Exception is thrown here***
  4. Dim ms As New MemoryStream(binary)
  5. Dim SizeToWrite As Integer = ChunkSize
  6.  
Here is the error:

Expand|Select|Wrap|Line Numbers
  1. Server Error in '/' Application.
  2. Conversion from type 'Byte()' to type 'Integer' is not valid.
  3. 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.
  4.  
  5. Exception Details: System.InvalidCastException: Conversion from type 'Byte()' to type 'Integer' is not valid.
  6.  
  7. Source Error:
  8.  
  9. Line 40:             Const ChunkSize As Integer = 1024
  10. Line 41:             Dim buffer() As Byte = New Byte(ChunkSize) {}
  11. Line 42:             Dim binary(reader("document")) As Byte
  12. Line 43:             Dim ms As New MemoryStream(binary)
  13. Line 44:             Dim SizeToWrite As Integer = ChunkSize
  14.  
and here is the Stack Trace:

Expand|Select|Wrap|Line Numbers
  1. [InvalidCastException: Conversion from type 'Byte()' to type 'Integer' is not valid.]
  2.    Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value) +987
  3.    HardCopyOrderForm.Download.btnPDF_Click(Object sender, EventArgs e) in ***************************************************************************************************************   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
  4.    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
  5.    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
  6.    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
  7.    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
  8.    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
  9.  
Nov 3 '11 #1
0 1335

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simon | last post by:
I need to get data from an MSSQL, stream it over network and load this data in a DataSet on a device running Compact Framework. I am able to stream byte array over network so now I need to stream...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
1
by: Angel Filev | last post by:
Hi everyone, I am trying to store a file as a binary array in an "image" field in SQL Server 2000 database. It works OK except for the ".PDF" files, which I believe get corrupted in the process...
10
by: Danny | last post by:
I am working on a project where I will receive xml documents from clients machines as a byte array. They will use the web browser navigate method to post the data to my ASP.NET page. I then pick up...
5
by: Trapulo | last post by:
Hello, I need to send to a webservice a parameter that is a string containing an XML doc. In this xml, a node value came from a byte array (it's an RSA signature). What is the best way to convert...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
3
by: gsuns82 | last post by:
HI all, I tried to read an array from an external javascript file called read.js,The array was declared & as well as defined with some values inside the <script> tag of a html file. For an...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.