hi everyone. I have this code to retrieve image from sql server using asp.net with vb, the problem is the image cannot display in the web page and I don't know what is the problem can you please help me.`

Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing
Public Class _Default
    Inherits System.Web.UI.Page
    Public sqlcon As SqlConnection = New SqlConnection("server=.; database=batool; Trusted_Connection=yes;")
    Public sqlcmd As SqlCommand
   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Try
            sqlcon.Open()
            sqlcmd = New SqlCommand("select photo from qq", sqlcon)
            Dim reader As SqlDataReader = sqlcmd.ExecuteReader
            If reader.Read Then
                Context.Response.BinaryWrite(DirectCast(reader("photo"), Byte()))
            End If
            sqlcon.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

`

Recommended Answers

All 6 Replies

Hi,

You're retreiving the image file as a binary object from the database and then you're trying to open directly in the browser?

Do you set the content Type in the response so the browser knows what you are sending it? e.g. Response.ContentType ="image/jpg"

yes, this is what I am trying to do but the image file that i save in the database is text and it is the location of the image.

Location as URL or path on server? You need to query it as a URL inside an <IMG HREF="..."> to get the browser to pick it up. You might need to create a service to get the file from path to URL, if it is a path. If you want to serve the image directly, you need to open the file and copy it to the browser socket after the http header that says it is content type image/jpg. It'd be nice to fetch the file size so the header can specify the size, but EOF on a simple socket is usually sufficient. For persistent connection http/1.1, length is probably mandatory.

Hi,

The code you've posted would be for if you were storing your image in a binary format in a database server.

If you are just holding the path to the file, then as David_50 said then why not just populate an image with the path?

thank you for reply me.
David_50 can you give me example to understand it.

You are writing an HTML page with your web service. Suppose the jpg file is in the web subtree, one way or another, and the web root relative path is in the database column. When composing the web page for such a situation, you need to create an element to make the browser fetch the jpg file, perhaps with something like this in SQL:
'<img src="' + rtrim(column) + '" alt="column_name" ... >'
Of course, options in the <img> (alt="column_name" ...) are up to your needs. I am a big fan of 'width="xx%"'; it works on all devices.

Now, if the file is outside the web root subtree or elsewhere, you need to have a server of some sort to fetch it and create a fuller corresponding URL for the <img> src attribute. It could be anonymous ftp, http, or whatever else browsers speak. If the database column value is the locally valid URL, the above works no matter where the file is. If the database column just has the base file name, you need to add prefixes to make it a valid local URL in the above. For instance "http://other_host/images_path/xxx.jpg" is a valid URL anywhere, being absolute, but "/images_path/xxx.jpg" is only a valid local URL for files in this local web server root subtree. If your database column just has "xxx" or "xxx.jpg", you need to provide the "http://other_host/images_path/" or "/images_path/" prefix in the constant string above, and for "xxx" a '.jpg' suffix.

Be a part of the DaniWeb community

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