locked
Display Image from sql 2005 with asp.net 2.0 VB RRS feed

  • Question

  • User1033186037 posted

    Hello, I am working with application, the database already created by the company and they have logos as binary data in sql 2005 database,

    I have copy of the database and I build small appliction for them on my local machine. Everythis is working fine except I can not display images from the databse.

     

    I used handler to display the image and I have image control on my page where do I want to display the image. I don't know the MIME-Type of the images and whatever I do, I just get red x as image. Here is my code. Can someone tell me if I am wrong or thsi has something to do with the data..

     Thanks in advance

    <%@ WebHandler Language="vb" Class="ShowImage" %>

     

    Imports System

    Imports System.Web

    Imports System.IO

    Imports System.Configuration

    Imports System.Collections

    Imports System.Data.SqlClient

    Imports System.ComponentModel

    Imports System.Web.Security

    Imports System.Web.UI

    Imports System.Web.UI.WebControls

    Imports System.Web.UI.WebControls.WebParts

    Imports System.Web.UI.HtmlControls

    Imports System.Drawing

    Imports System.Drawing.Imaging

     

    Public Class ShowImageImplements IHttpHandler

     

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

     

    Dim conn As String = ConfigurationManager.ConnectionStrings("myconnectionstrinname").ConnectionString

    Dim connection As SqlConnection = New SqlConnection(conn)

     

    Dim sql As String = "SELECT OfficeLogo FROM EmployeeOffice WHERE OfficeID = 'X' "

    Dim cmd As SqlCommand = New SqlCommand(sql, connection)

    connection.Open()

     

    Dim dr As SqlDataReader = cmd.ExecuteReader()

    dr.Read()

     

    context.Response.ContentType = "image/jpeg"     ' I used all other mimetype but no work

    context.Response.BinaryWrite(DirectCast((dr("OfficeLogo")), Byte()))

    End Sub

     

     

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

    Get

    Return False

    End Get

    End Property

     

     

    End Class

     

     

    Friday, February 6, 2009 3:21 PM

All replies

  • User1033186037 posted

    I have changed my handler to .aspx page and here is the code but still have the same problem with the image displaying

    only red x display

    Do you see any problem with this code?

     

     

     

        Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

            Dim PictureID As Char = Convert.ToChar(Request.QueryString("PictureID"))

         
            Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("HelloUSAConnection").ConnectionString)
            myConnection.Open()

            Dim cmdSurveyLogo As New SqlCommand("spGetSurveyOfficeLogo", myConnection)

            cmdSurveyLogo.CommandType = Data.CommandType.StoredProcedure

            cmdSurveyLogo.Parameters.Add(New SqlParameter("@OfficeID", Data.SqlDbType.VarChar, 1))
            cmdSurveyLogo.Parameters("@OfficeID").Value = PictureID

            Dim myReader As SqlDataReader = cmdSurveyLogo.ExecuteReader

            If myReader.Read Then

                Response.ContentType = "Image/jpeg"
                Response.BinaryWrite(DirectCast(myReader("OfficeLogo"), Byte()))

            End If
            myReader.Close()
            myConnection.Close()

    Thursday, February 12, 2009 3:44 PM