PACMAN
PACMAN

Reputation: 41

How to get/load blob image using

Im trying to load an image, field of longblob and I have a 1 row(id,design,name) data in my table. but when I click the button to show the image and it not showing it.

I have a GetImage.aspx to call my method

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        Using conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
            cmd = New MySqlCommand("SELECT design FROM mytable")
            Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
            conn.Open()
            Context.Response.Clear()
            Context.Response.ContentType = "image/jpeg"
            Context.Response.BinaryWrite(imageData)
            Context.Response.End()

        End Using

    Catch ex As Exception
    End Try
End Sub

And this is my control button to show the image from my default page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Image1.ImageUrl = "GetImage.aspx?"
End Sub

Please help I dont understand why my image is not showing

Upvotes: 0

Views: 144

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416081

These lines are out of order:

Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
conn.Open()

You need to open the connection before you execute the command:

conn.Open()
Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())

You also need to associate the connection with the command:

cmd = New MySqlCommand("SELECT design FROM mytable", conn)

Upvotes: 1

Related Questions