Solved

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

Posted on 2011-03-22
9
1,270 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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 29

Expert Comment

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

Expert Comment

by:saragani
Comment Utility
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
Comment Utility
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
Comment Utility
I think I am going to take another approach entirely.

Thanks everybody...
0
 

Author Closing Comment

by:vbnetcoder
Comment Utility
ty
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now