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...

Recommended Answers


I quote: "The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty. Returns a maximum of 2033 characters."

Perhaps it's not returning the full image stream.

Jump to Post

The documentation I saw says that longblob is stored as byte strings. Executescalar returns a single value.Look at this one http://www.dhirajranka.com/2011/05/image-to-database-and-from-database-to-picturebox-control-c/

Jump to Post

All 5 Replies

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.

Have you tried?

 PictureBox1.Image = ms
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.