Solved

ERROR:System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'.

Posted on 2011-03-22
9
1,423 Views
Last Modified: 2012-05-11
I get this error:

System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'.

On this line of CODE:

 Using ms As New System.IO.MemoryStream(DirectCast(row(3), Byte()))

Row(3)  originates from a image field in the database. I then put it into a dataviewgrid and then took the values of the dataviiew grid and put it into a dataset....

Why the error?
0
Comment
Question by:vbnetcoder
  • 4
  • 3
  • 2
9 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35195383
Why you decided your row(3) is a byte array?

The best way here to resolve the problem, set a breakpoint on this line and check what the type of the object.
Or print the type to console/debug window:

Debug.WriteLine("The type of the row is: " & row(3).GetType.ToString)

I can bet it's not a byte array...
0
 

Author Comment

by:vbnetcoder
ID: 35195447
It's not its a string....

The byte array image show as the grid now but when I created a dataset from my datagrid view it did not translate

I never used these byte arrays before so don't know what they are all about
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35195528
"The byte array image show as the grid now"...

I can't understand...

Anyway, the question you asked is answered. You can't call directcast for a wrong type. Let me think on a solution nevertheless...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 35195535
OK, all I can help I can give you a way to convert a string to a byte array. You may try to use this byte array for your stream...
string s = "abcd";
            byte[] bytes = new byte[s.Length*2];
            int j = 0;
            for(int i = 0; i<s.Length; i++)
            {
                bytes[j++] = (byte)(s[i]&255);
                bytes[j++] = (byte)(s[i]>>8);
            }

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35195728
Yeah... The code is in C#. You may try to convert it to VB yourself...
0
 
LVL 11

Expert Comment

by:saragani
ID: 35196149
anarki, why not use Encoding?

For example:

Encoding.ASCII.GetBytes("abcd");
or
Encoding.Default.GetBytes("abcd");
or
Encoding.Unicode.GetBytes("abcd");


Depending on the type of string you are expecting....
0
 
LVL 11

Expert Comment

by:saragani
ID: 35196158
Btw vbnetcoder,

Without knowing your DB structure and seeing your code, it is hard to guess what is wrong... but the .Net framework will not just tell you that you are trying to cast string to image bytes.

Maybe the database has the image saved as string (location to file) and you are trying to set bitmap bits from a string instead of giving it the actual data... I don't know, and I can't really know without seeing any code.
0
 

Author Comment

by:vbnetcoder
ID: 35197602
I think I am going to take another approach entirely.

Thanks everybody...
0
 

Author Closing Comment

by:vbnetcoder
ID: 35296303
ty
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question