troubleshooting Question

Showing image in image control using vb.net and asp.net

Avatar of rckrch
rckrchFlag for United States of America asked on
ASP.NETVisual Basic.NET
18 Comments1 Solution3255 ViewsLast Modified:
I am having a lot of difficulty trying to get the image that is stored in a database to show up in the image control on my web page.  I have successfully loaded the image into the database with the below code, and retrieving it is not a problem either - except that I cannot get the image into the image control.  I have tried mutiple different codes to get the image to load into the image control, but nothing has worked.

Can anyone help?  Below is the code snipett.

Any help would be greatly appreciated.

Thanks

asp code for the image control
             <asp:Image ID="ProfileImg" runat="server"
                            BorderStyle="None" BorderColor="Transparent"
                            ImageUrl="" Width="30%"
                            Height="50%" />

This part of the code to load the image data into database works fine:

*******************************************
Protected Sub UploadWhiteImg_Click() Handles UploadWhiteImg.Click
        If (WhiteUpload.HasFile) Then
            Dim savePath As String = "C:\My Documents\"
            Dim fileName As String = WhiteUpload.FileName
            Dim pathToCheck As String = savePath + fileName          
            Dim tempfileName As String = "White" & DieNoDrpDnLst.SelectedValue  
            fileName = tempfileName            

            WhiteUpload.SaveAs(pathToCheck)
        Else
           MessageBox("You did not specify a file to upload.")
        End If
        Dim FileText As String = WhiteUpload.PostedFile.FileName.ToString
        Dim dbcon2 As New SqlConnection("Data Source=...")
        Dim dbcmd2 As New SqlCommand("INSERT INTO PROFILEIMAGES (ProfileImage, DieNumber) VALUES (@ProfileImage, @DieNumber)")
        dbcon2.Open()
        dbcmd2.Connection = dbcon2
        Using ProfileImage As System.Drawing.Image = System.Drawing.Image.FromFile(WhiteUpload.PostedFile.FileName)
(WhiteUpload.PostedFile.FileName.LastIndexOf("\") + 1))
            Using stream As New IO.MemoryStream
                ProfileImage.Save(stream, Imaging.ImageFormat.Jpeg) 'Jpeg
                dbcmd2.Parameters.Add("@ProfileImage", SqlDbType.VarBinary).Value = stream.GetBuffer()
                dbcmd2.Parameters.AddWithValue("@DieNumber", DieNoDrpDnLst.SelectedValue)
            End Using
        End Using
        dbcmd2.ExecuteNonQuery()
        dbcmd2.Dispose()
        dbcon2.Close()
        dbcon2.Dispose()


    End Sub

*************************************
The below code is intended to return the image with a drop down list selectedindexchanged event.  It will return the image, but not in the image control.  It returns the image filling the entire screen.  I am just trying to load the image into the image control.

Protected Sub DieNoDrpDnLst_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DieNoDrpDnLst.SelectedIndexChanged

Dim dbconimg As New SqlConnection("Data Source=io.....")
        Dim dbcmdimg As New SqlCommand("SELECT ProfileImage FROM PROFILEIMAGES WHERE DieNumber =" & DieNoDrpDnLst.SelectedValue)
        dbconimg.Open()
        dbcmdimg.Connection = dbconimg

Dim imageData As Byte() = DirectCast(dbcmdimg.ExecuteScalar(), Byte())          Context.Response.ContentType = "image/jpeg"
        Context.Response.BinaryWrite(imageData)           'ProfileImg.DataBind()
       

        dbcmdimg.Dispose()
        dbconimg.Close()
        dbconimg.Dispose()

End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 18 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 18 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros