Question BinaryWrite does not show binary image from DB

JustJF

New member
Joined
Nov 1, 2009
Messages
1
Programming Experience
10+
Trying to display a varbinary image from SQL Server into an ASPX page, but all I get is a little red "x" instead of the image. I have confirmed that the datareader is returning a valid binary string from the database, and that bytImage is being correctly populated. Help!

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' This page simply displays the requested picture in an empty ASPX page.

Dim cnDBGeneric As SqlConnection
Dim cmdGeneric As SqlCommand
Dim drGeneric As SqlDataReader
Dim strSQLSelect As String

'Connect to the database and bring back the image contents
cnDBGeneric = New SqlConnection
cnDBGeneric.ConnectionString = ConfigurationManager.AppSettings("DB_AMCG_AMCG_Main")
strSQLSelect = "SELECT [LFOwnerLogo], [LFOwnerLogoFilename] " & _
"FROM [" & Request.QueryString("imgLocation") & "] " & _
"WHERE [ID]=" & Request.QueryString("imgRecordID")
cmdGeneric = New SqlCommand
cmdGeneric.Connection = cnDBGeneric
cmdGeneric.CommandText = strSQLSelect
cnDBGeneric.Open()
drGeneric = cmdGeneric.ExecuteReader

If drGeneric.Read Then

Dim bytImage(drGeneric.GetBytes(0, 0, Nothing, 0, Integer.MaxValue)) As Byte
drGeneric.GetBytes(0, 0, bytImage, 0, bytImage.Length)

Response.Clear()
Response.Buffer = True
Response.ClearContent()
Response.ClearHeaders()

Select Case Right(drGeneric("LFOwnerLogoFilename").ToString, 3)
Case "jpg"
Response.ContentType = "image/jpeg"
Case Else
Response.ContentType = "image/ " & _
Right(drGeneric("LFOwnerLogoFilename").ToString, 3)
End Select

Response.BinaryWrite(bytImage)
Response.End()


Else ' No data returned from search
Response.Write("No image found!")
End If

drGeneric.Close()
cnDBGeneric.Close()

End Sub
 
Back
Top