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

Hi All,

I am trying to retrieve a Picture from an SQL 2005 database. Here is my code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ListingID As String = (Request.QueryString("GUID"))

'Connect to the database and bring back the image contents & MIME type for the specified picture

Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionStringListings").ConnectionString)

Const SQL As String = "SELECT [Pic1Ext], [Picture1] FROM [Listings] WHERE [ListingID] = @ListingID"

Const SQL As String = "SELECT [Pic1Ext], [Picture1] FROM [Listings] "Dim myCommand As New SqlCommand(SQL, myConnection)myCommand.Parameters.AddWithValue("@ListingID", ListingID)


Dim myReader As SqlDataReader = myCommand.ExecuteReader

If myReader.Read Then

Response.ContentType = myReader("Pic1Ext").ToString() Response.BinaryWrite(myReader("Picture1"))

End If



End Using

End Sub

And I am getting the following error and I am not sure what it means??

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

Source Error:

Line 22:             If myReader.Read Then
Line 23:                 Response.ContentType = myReader("Pic1Ext").ToString()
Line 24:                 Response.BinaryWrite(myReader("Picture1"))
Line 25:             End If
Line 26:             myReader.Close()

Source File: D:\inetpub\rentication\Rentication0\RegisteredUsers\ShowPicture.aspx.vb    Line: 24


I know I have a picture uploaded but I keep getting this error. Help?

5/5/2008 5:18:59 AM 3721 articles. 0 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 13
Get it on Google Play
Get it on Apple App Store

what is the datatype of [Picture1] in [Listings] table? it should be of type "image"..

Public Sub Page_Load(sender As Object, e As EventArgs)

        Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        Dim myCommand As New SqlCommand("Select * from Person", myConnection)

            Dim myDataReader as SqlDataReader
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

            Do While (myDataReader.Read())
                Response.ContentType = myDataReader.Item("PersonImageType")

            Response.Write("Person info successfully retrieved!")
        Catch SQLexc As SqlException
            Response.Write("Read Failed : " & SQLexc.ToString())
        End Try

    End Sub


[KaushaL] || BloG || Profile || Microsoft MVP

"I would love to change the world, but they won’t give me the source code"

Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
5/5/2008 7:49:03 AM

Thanks for responding,

I am using VS 2008 I don't believe that there is an image type in this platform, I am using nvarchar(max) in the database.

5/5/2008 3:00:37 PM

Ok I have an even bigger problem now. It turns out I have not been insertimg images properly, they have been inserting as string in my database in a row with type varchar(MAX). I no wunderstand that I have to make that row varbinary(Max). But when I do that I can't insert anything anymore. I think I have a discrepancy in variable types.

This is my code that sets the variable:

Dim imageBytes(F1.PostedFile.InputStream.Length) As Byte

F1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)

e.Values("Picture1Data") = imageBytes

The SQL Parameter is defined like this:

<asp:Parameter Name ="Picture1Data" type="Object"/>

<asp:Parameter Name ="MIMEType1" />


When I have the "Type=Object" in the parameter control, I am able to insert other fields into the database but not Picture1?

When I remove it nothing inserts at all?

Any help, I hope I have explained my problem well enough.

5/6/2008 5:04:25 AM