2

This should be pretty simple for a pro. I have images in sql server database and I want to retrieve them in my aspx (vb.net) file. I have in aspx this image control - in vb.net i have started this code -

Private Sub ImageDisplay()
    Dim SqlCnn As SqlConnection = Nothing, sql As String = ""
    ConnectDB(SqlCnn)
    Try
        sql = "SELECT image FROM employees (NOLOCK) WHERE ID =" & emp_id
        sqlcmd = New SqlCommand(sqlstr, SqlCnn)
        Dim imageData As Byte() = DirectCast(sqlcmd.ExecuteScalar(), Byte())
        Dim newImage As Image = Nothing
        If Not imageData Is Nothing Then
            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                ms.Write(imageData, 0, imageData.Length)
                newImage = Image.FromStream(ms, True)
            End Using
            image1.Image = newImage
        End If
    Catch ex As Exception
        ReportError(ex)
    Finally
        CloseDB(SqlCnn)
    End Try
End Sub

I am getting error on 2 places. - newImage = Image.FromStream(ms, True) - image1.Image = newImage Fromstream is not a member of system.web.ui.webcontrols.image and second error Image is not a member of system.web.ui.webcontrols.image

Mineko
  • 641
  • 2
  • 14
  • 21
ferdd
  • 21
  • 1
  • 1
  • 3
  • nobody?? cmon it cant be that hard!!! – ferdd Mar 10 '10 at 00:00
  • You have to be a bit patient. People will answer when they happen to see your question, they don't sit around 24-7 just waiting for it. – Guffa Mar 10 '10 at 00:06
  • haha np. anywyas i am trying this code but going wrong somewhere. let me edit the question. – ferdd Mar 10 '10 at 00:12

2 Answers2

2

You need both a command object and a data reader. However, you are putting them in the wrong page.

If you check out the properties of the Image control you will see that it doesn't have any Image property, so you can't load an image and put in the control. The reason for that is that you can't send both the page and the image in the same response, instead the browser has to request the image separately when the page loads.

To get the image from the database and show in the web page you need a separate proxy page that you can use to get only the image data from the database. In the ImageUrl property of the Image control you would put something like "GetImage.ashx". Then you make an HTTP handler with that name that will just get the image data from the database and write to the response stream.

Example:

Private Sub HandleRequest(context as HttpContext)
  Dim SqlCnn As SqlConnection = Nothing, sql As String
  Dim emp_id As Integer
  emp_id = Int32.Parse(context.Request.QueryString("id"))
  ConnectDB(SqlCnn)
  Try
    sql = "SELECT image FROM employees (NOLOCK) WHERE ID =" & emp_id
    sqlcmd = New SqlCommand(sqlstr, SqlCnn)
    Dim imageData As Byte() = DirectCast(sqlcmd.ExecuteScalar(), Byte())
    context.Response.ContentType = "image/jpeg"
    context.Response.BinaryWrite(imageData)
  Catch ex As Exception
    ReportError(ex)
  Finally
    CloseDB(SqlCnn)
  End Try
End Sub
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1
Dim cn As SqlConnection

    cn = New SqlConnection
    cn.ConnectionString = "Data Source=UMAR\UMAR;Initial Catalog=DMCHS;Integrated Security=True"

    Dim cmd As New System.Data.SqlClient.SqlCommand("select D1 from DBFile where mem_no=2")
    cmd.Connection = cn
    cmd.CommandType = CommandType.Text

    Dim ImgStream As New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte()))

    PictureBox1.Image = Image.FromStream(ImgStream)

    ImgStream.Dispose()

    cmd.Connection.Close()
  • if you need to view other examples then please visit here [link : https://www.dotnetheaven.com/article/retrieve-an-image-from-database-in-vb.net] – Vijay Raval Feb 01 '19 at 10:54