When I retrieve image from mysql longblob field to vb.net picturebox I am getting an error.. pls help..

       Dim SqlString As String = "select image from employeesmaster where name='VENU'"
        Dim cmd As New MySqlCommand(SqlString, MyConnection)
        Dim ImageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
        'Dim ms As New IO.MemoryStream(ImageData)
        If Not ImageData Is Nothing Then
            Dim ms As New IO.MemoryStream(ImageData)
            Me.PictureBox1.Image = Image.FromStream(MS)
            Me.PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
            MsgBox("no pict")
        End If

print scrn image of error attached...

Edited 1 Year Ago by vgxp

Attachments Error1.jpg 80.51 KB

I haven't got SQL installed at the moment so I can't code and test something but I have used the following

Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click

    Dim con As New SqlConnection("Server=.\SQLEXPRESS;Database=mydb;Trusted_Connection=yes;")
    Dim cmd As New SqlCommand("", con)

    Dim file As String = "D:\temp\test.docx"

    cmd.CommandText = "SELECT * FROM WordDocs WHERE FileName = @FILE"
    cmd.Parameters.AddWithValue("@FILE", file)


    Dim rdr As SqlDataReader = cmd.ExecuteReader
    If rdr.Read Then
        WriteBinary(file, rdr("WordDoc"))
        MsgBox(file & " not found")
    End If


End Sub

to retrieve binary data. Perhaps this will give you an idea.

Agreed with @pritaeas, Dim ImageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte()) never returns any data from database.
You must have to read the field value of the database through MySqlDataReader() Class and then it can show the image from MemoryStream.
Hope it can give a way to do your job.

This article has been dead for over six months. Start a new discussion instead.