Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-03-22
9
Medium Priority
?
1,726 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 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

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!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 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