473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Converting object to byte array

Hi, I experience some troubles trying to convert an object,returned by a DataTable for a field value to its actual representation of Byte[]. The field values in a DataTable as we all know are returned as instance of the Object class and it is up to the developer to cast it to its actual type.
In my case i access a field of type text in the database and obviously the driver converts it into byte array. Unfortunaletly a construct like this
Array arr = (Array)dt.Rows[0]["FieldName"
always results in a CastNotValidException
However the dt.Rows[0]["FieldName"].ToString() returns "System.Bute[]", which actually prompts for the actual type
Any hints on how to solve this problem appreciated
Thanks in advance
Svetoslav
Nov 15 '05 #1
4 3855
byte[] fieldXXXValue = (byte[]) dt.Rows[0]["FieldXXXName"];

Thought I'm not sure why you can't cast it all the way down to an array.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Svetoslav Vasilev" <an*******@discussions.microsoft.com> wrote in message
news:75**********************************@microsof t.com...
Hi, I experience some troubles trying to convert an object,returned by a DataTable for a field value to its actual representation of Byte[]. The field
values in a DataTable as we all know are returned as instance of the Object
class and it is up to the developer to cast it to its actual type. In my case i access a field of type text in the database and obviously the driver converts it into byte array. Unfortunaletly a construct like this: Array arr = (Array)dt.Rows[0]["FieldName"]
always results in a CastNotValidException.
However the dt.Rows[0]["FieldName"].ToString() returns "System.Bute[]", which actually prompts for the actual type. Any hints on how to solve this problem appreciated.
Thanks in advance.
Svetoslav

Nov 15 '05 #2
byte[] fieldXXXValue = (byte[]) dt.Rows[0]["FieldXXXName"];

Thought I'm not sure why you can't cast it all the way down to an array.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Svetoslav Vasilev" <an*******@discussions.microsoft.com> wrote in message
news:75**********************************@microsof t.com...
Hi, I experience some troubles trying to convert an object,returned by a DataTable for a field value to its actual representation of Byte[]. The field
values in a DataTable as we all know are returned as instance of the Object
class and it is up to the developer to cast it to its actual type. In my case i access a field of type text in the database and obviously the driver converts it into byte array. Unfortunaletly a construct like this: Array arr = (Array)dt.Rows[0]["FieldName"]
always results in a CastNotValidException.
However the dt.Rows[0]["FieldName"].ToString() returns "System.Bute[]", which actually prompts for the actual type. Any hints on how to solve this problem appreciated.
Thanks in advance.
Svetoslav

Nov 15 '05 #3
I tried this approach first and it did not go through either. It resulted with the same exception
Svetoslav
Nov 15 '05 #4
I tried this approach first and it did not go through either. It resulted with the same exception
Svetoslav
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
5
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
2
by: Matt | last post by:
I just wanted to know if I am converting to/from streams the easiest and correct way. I am performing the following statements to convert byte arrays to and from streams during different...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
5
by: Lint Radley | last post by:
Hello, I am looking for a way to cast a byte to Int16. I get this error: "Unable to cast object of type 'System.Byte' to type 'System.Int16'" If you're curious, I am using a library for...
2
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.