Hi,
Can someone help me in how I can retreive my images from my SQL DB using a
GridView or DetailsView?
I can store my image into my DB, so I would like to retreive it, but it
doesn't seem to work.
I'm using a Handler (ashx file) to retreive the image, but it doesn't seem
to work.
Here is my VB.NET code:
<%@ WebHandler Language="VB" Class="Handlerg" %>
Imports System
Imports System.IO
Imports System.Web
Imports System.Collections
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Public Class Handlerg
Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean
Get
Return True
End Get
End Property
Public Shared Function GetPhoto2(ByVal BuyGroundID As Integer) As Stream
' Using
Dim connection As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrin gs("IMMOASPNETDBConnectionString").ConnectionStrin g)
Try
' Using
Dim command As SqlCommand = New SqlCommand("select [Image1] FROM
[aspnet_BuyGroundImages] WHERE ([BuyGroundID]= @BuyGroundID)", connection)
Try
command.Parameters.Add(New SqlParameter("@BuyGroundID",
BuyGroundID))
connection.Open()
Dim result As Object = command.ExecuteScalar
Try
Return New MemoryStream(CType(result, Byte()))
Catch
Return Nothing
End Try
Finally
CType(command, IDisposable).Dispose()
End Try
Finally
CType(connection, IDisposable).Dispose()
End Try
End Function
Public Sub ProcessRequest(ByVal context As HttpContext)
context.Response.ContentType = "image/jpeg"
context.Response.Cache.SetCacheability(HttpCacheab ility.Public)
context.Response.BufferOutput = False
Dim id As Int32 = -1
Dim stream As Stream = Nothing
id = Convert.ToInt32(context.Request.QueryString("BuyGr oundID"))
stream = GetPhoto2(id)
Const buffersize As Integer = 1024 * 16
Dim buffer2(buffersize) As Byte
Dim count As Integer = stream.Read(buffer2, 0, buffersize)
While count > 0
context.Response.OutputStream.Write(buffer2, 0, count)
count = stream.Read(buffer2, 0, buffersize)
End While
End Sub
End Class
Thanks for all your help....
Bart