Retreiving images from SQL Express 2005

G

Guest

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.ConnectionStrings("IMMOASPNETDBConnectionString").ConnectionString)
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(HttpCacheability.Public)
context.Response.BufferOutput = False
Dim id As Int32 = -1
Dim stream As Stream = Nothing
id = Convert.ToInt32(context.Request.QueryString("BuyGroundID"))
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top