Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversion error trying to display image from SQL Server

Status
Not open for further replies.

atruhoo

Programmer
Jan 8, 2003
131
US
I am trying to download and display an image that I had previously uploaded to SQL Server database and keep getting this error below.

Value of type '1-dimensional array of System.Object' cannot be converted to '1-dimensional array of Byte' because 'System.Object' is not derived from 'Byte'

Here is the code I am working with, I would appreciate any input. I am sure it is something simple that I am missing.

Private Sub btnDownloadImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownloadImg.Click
Try
Dim strConn As String = "Data Source=RSTACY260;User ID=sa;Initial Catalog=UpLoadImage;Password=atruhoo"
Dim conn As New SqlConnection(strConn)
conn.Open()
Dim cmdDownload As New SqlCommand("select * from TestImageTable")
cmdDownload.Connection = conn
cmdDownload.CommandType = CommandType.Text

Dim da As New SqlDataAdapter(cmdDownload)
Dim ds As New DataSet
da.Fill(ds)

Dim bits As Byte
bits = CType(ds.Tables(0).Rows(0).ItemArray, Byte())
Dim memorybits As New MemoryStream(bits)
Dim bitmap As New Bitmap(memorybits)
PictureBox1.Image = bitmap
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
 
I guess

Dim bits() As Byte


Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 

Thanks Christiaan. I though about that late last night, though still had problem with image displaying when started looking at this morning. With a little more research and playing around I got it to work (see changes below).

Dim bits() As Byte
bits = CType(ds.Tables(0).Rows(ListBox1.SelectedIndex)("SampleImage"), Byte())

Dim memorybits As New MemoryStream(bits)
With PictureBox1
.Image = Image.FromStream(memorybits)
.SizeMode = PictureBoxSizeMode.AutoSize
End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top