[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register 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,659 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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