Solved

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

Posted on 2011-03-22
9
1,519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 30

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 30

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

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 30

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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