Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends While i m going to run my page i am getting the

Error : Unable to cast object of type 'System.String' to type 'System.Byte[]'. like this, will you please give me a help please

code here

Dim TableContainer As New DataTable
Try
Dim getvalues As New Select_Property()
Dim ReturnVar As Integer = 1
BusinessLayer = New BLL()

values.FLAG = "EMAIL"
getsetvalues.order_No = MessageID

TableContainer = BusinessLayer.GenerateData(values, func_amc(sess_id), strUserCode, strEqyPage, "")

If TableContainer.Rows.Count > 0 Then
Dim attachment As String = "attachment; filename= _received.eml"
Response.ContentType = "application/windows live mail 2012"
Response.AddHeader("Content-Disposition", attachment)
Response.ClearContent()
Response.Charset = ""

If (Not TableContainer.Rows(0).Item(0) Is DBNull.Value) Then
MailBLOB = CType(TableContainer.Rows(0).Item(0), Byte())
End If
Response.OutputStream.Read(MailBLOB, 0, MailBLOB.Length)
TableContainer = Nothing
Else
ClientScript.RegisterStartupScript(Me.GetType(), "Message", "<script> alert('No E-mail Attached');</script>")
End If
'End If
Catch ex1 As Threading.ThreadAbortException
Catch e As Exception
Finally
BusinessLayer.Dispose()
End Try

Regrds ,
Velsamy A
Posted
Updated 20-Apr-14 18:36pm
v2
Comments
Tom Marvolo Riddle 17-Apr-14 0:19am    
post your code
Sanket Saxena 17-Apr-14 0:46am    
cant help without seeing your code.
avelsamy 17-Apr-14 6:42am    
Dim TableContainer As New DataTable
Try
Dim getvalues As New Select_Property()
Dim ReturnVar As Integer = 1
BusinessLayer = New BLL()

values.FLAG = "EMAIL"
getsetvalues.order_No = MessageID

TableContainer = BusinessLayer.GenerateData(values, func_amc(sess_id), strUserCode, strEqyPage, "")

If TableContainer.Rows.Count > 0 Then
Dim attachment As String = "attachment; filename= _received.eml"
Response.ContentType = "application/windows live mail 2012"
Response.AddHeader("Content-Disposition", attachment)
Response.ClearContent()
Response.Charset = ""

If (Not TableContainer.Rows(0).Item(0) Is DBNull.Value) Then
MailBLOB = CType(TableContainer.Rows(0).Item(0), Byte())
End If
Response.OutputStream.Read(MailBLOB, 0, MailBLOB.Length)
TableContainer = Nothing
Else
ClientScript.RegisterStartupScript(Me.GetType(), "Message", "<script> alert('No E-mail Attached');</script>")
End If
'End If
Catch ex1 As Threading.ThreadAbortException
Catch e As Exception
Finally
BusinessLayer.Dispose()
End Try
Sergey Alexandrovich Kryukov 17-Apr-14 13:04pm    
You can put this code in your question, using "Improve question". Please don't put code in comments, as they are not so readable.
—SA
Sergey Alexandrovich Kryukov 17-Apr-14 1:47am    
Okay, the line of code showing this error is extremely silly. That's all what anyone can say based on your information.
Case closed.

With such questioning skills you hardly can hope for any real help.

—SA

The problem is in below line of code

MailBLOB = CType(TableContainer.Rows(0).Item(0), Byte())


Is there any specific reason why you are converting to byte. What are you trying to do with MailBLOB object. I do not see any further code which explains why MailBLOB is used. If it is not used for any specific purpose then you can keep MailBLOB as string itself.

After looking at your comments, first you can convert the mailblob to byte data using getbytes method

http://www.techrepublic.com/blog/software-engineer/convert-a-string-into-a-byte-array-in-vbnet/#.[^]

This way you can save data to db. Then to fetch the data you can use below conversion

C#
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(row["DOCLIST"]);
String s = System.Text.Encoding.UTF8.GetString(byteBLOBData);
 
Share this answer
 
v2
Comments
avelsamy 21-Apr-14 1:20am    
i am trying to substitute the table container values to mailBLOB(byte type) then i want to open the mail, bcoz the mail details are stored as a blob type

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900