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

angel

Newcomer
Joined
Nov 19, 2006
Messages
1
Pls. help me with this error...

'Read the contents in to a byte array
Dim objMyData() As Byte
objMyData = CType(objMyRow("Document"), Byte()) <<<<<<<ERROR

Dim objMyFile As New FileStream("C:\temp\document.doc", FileMode.OpenOrCreate, FileAccess.Write)
objMyFile.Write(objMyData, 0, objMyData.Length)


Unable to cast object of type 'System.String' to type 'System.Byte[]' in vb.net

WHERE Document is a column in table in sql2005 that has a varchar(max) dataype.

I've tried converting it to
'Dim encoding As New System.Text.ASCIIEncoding()
'objMyData = encoding.GetBytes(objMyRow("Document"))

but the resulting document has an ascii format.

Thank you in advance....
 
There are other encoding classes under System.Text you could use, System.Text.UnicodeEncoding or one of the System.Text.UTFxxEncoding classes instead.

Alternatively you might try using a StreamWriter to write the strings directly to the file rather than handling the encoding yourself.
 
Back
Top