Displaying an image from the database.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I am trying to display images that are stored in the database on my web page. From what I have read, I should get the images and display them on one page, and then link my image control to the page i.e.
Photo.aspx would contain the code for downloading and displaying my image.
Code:
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            OpenConnection()
            sqlCmd = New SqlCommand
            sqlCmd.Connection = conSql
            sqlCmd.CommandText = "SELECT Picture, ImageType FROM myImages WHERE Identification = " & Request.QueryString("ID").ToString
            Dim myDataReader As SqlDataReader
            myDataReader = sqlCmd.ExecuteReader
            Do While (myDataReader.Read())
                Response.ContentType = myDataReader.Item("ImageType")
                Response.BinaryWrite(myDataReader.Item("Picture"))
            Loop
            myDataReader.Close()
            Response.Redirect("default.aspx", False)
        Catch ex As Exception
        Finally
            CloseConnection()
        End Try
    End Sub

While the page default.aspx would have an image control imgPicture, whose imageUrl is set to the page Photo.aspx. The only problem is that I cannot get/don't know how to get the page_load event to execute.

Mike55
 
Ok, after a lot of cursing and swearing at the computer and the world in general, I sorted out a solution. On the default.aspx add an html Image control, set its runat property to "server", set the id and name property to "image2" (you can use anything you like). In your code, when you want to display a picture for a particular person, call the following line of code:
Code:
image2.src = "photo.aspx?id=" & me.txtImage.text
where txtImage is a text box where you specify the id number of the picture you wish to display.

Mike55.
 
Back
Top