0

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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

I already tried. But it still not showing my image file. Di i have a problem to my button when i am using this? Image1.ImageUrl = "GetImage.aspx?". Thans
Do you see the image if you go directly to that url in your browser?
You mean if I run the GetImage? I run it but there is no image showing. But in my mysql database when I view it has a content image JPEG.
What happens if you remove the try/catch? You're swallowing the exception, and any information it might have about what went wrong.
yes! there is it gives me Connection must be valid and open. at this part "Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.