Retrieving images from the database & displaying

mike55

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

I am storing images in my database, where I am using 3 columns for each image, 1. imageName as an nvarchar(50), 2. imageData as an image, and 3. imageType as an nvarchar(100).

I have successfully inserted the images into the database, only problem that I am having is that I am unable to do any sort of compression on the images, but that's a different problem. The issue that I am having is that if I try and retrieve two images from the database and display them at the same time, in some cases only 1 image is displayed while in other cases no image is displayed.

To display the images, I have created a web control with two html image controls with runat="server", both these controls have the image url set to a different .aspx page which use the following code to retrieve the image from the database and display it:
Code:
Private Sub LoadImage()
        Dim myReader As Byte()
        Dim content As String = String.Empty

        myReader = dbConnection.RetrievePhoto(Request.QueryString("pid").ToString, 0, content)

        Try
            Response.ContentType = content
            If Not myReader Is Nothing Then
                Response.BinaryWrite(myReader)
            Else
                Response.WriteFile("images/Notfound.jpg")
            End If
        Catch ex As Exception

        End Try
    End Sub

In the event that the images don't display, I have manually gone to the pages which runs the above code, and I have confirmed that the code is running correctly and displaying the images.

Any suggestions on how to correct this problem? I think that it may be down to the size of the images, so they are causing some sort of timeout during execution of my code, but I cannot be positive.

Mike55.
 
Here it is:
Code:
Public Shared Function RetrievePhoto(ByVal id As String, ByVal photo As Int16, ByRef content As String) As Byte()
        Dim sqlCmd As SqlCommand
        connection = New SqlConnection(consql)

        Try
            'Open the database connection.
            If connection.State = ConnectionState.Closed Then
                connection.Open()
            End If
        Catch ex As Exception
            GoTo end1
        End Try

        Try
            myDsDataset = New DataSet
            myAdapter = New SqlDataAdapter
            Dim myDataReader As SqlDataReader = Nothing
            'Retrieve the data.
            Select Case photo
                Case 0
                    sqlCmd = New SqlCommand("SELECT (Pict1) as Pict, (Content1) as Content FROM Lands where ref = " & id & "", connection)
                    myDataReader = sqlCmd.ExecuteReader
                Case 1
                    sqlCmd = New SqlCommand("SELECT (Pict2) as Pict, (Content2) as Content FROM Lands where ref = " & id & "", connection)
                    myDataReader = sqlCmd.ExecuteReader
            End Select

            Dim myArray As Byte() = Nothing

            Do While myDataReader.Read
                If myDataReader("Pict") Is System.DBNull.Value Then
                    myArray = Nothing
                    GoTo xyz
                End If
                myArray = myDataReader.Item("Pict")
                content = myDataReader.Item("Content")
            Loop
xyz:
            'Return the data back to the user.
            Return myArray

        Catch ex As Exception
        Finally
            'Close the database connection.
            If connection.State = ConnectionState.Open Then
                connection.Close()
            End If
        End Try
end1:
    End Function

Mike55.
 
Nothing obvious jumps out as being a potential cause of the problem....

How large are the images roughly and if they are large what format are they being stored in (jpeg, png etc.) as time-outs are probably the main culprit here.
 
Back
Top