Click here to Skip to main content
16,019,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello wonderfull community please i have a problem.I am trying to display an image which is stored on a database on a gridview.The problem is that the image don't show only an image icon is displayed on the gridview.Please i don't know what i am doing wrong.I am using an httphandler page which is called from the gridview.Below are my code,please i need help.Thanks

gridview markup


ASP.NET
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Showemployeeinfo.aspx.vb" Inherits="Showemployeeinfo" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="EmployeeID">
        <columns>
            <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="First_Name" HeaderText="First_Name" 
                SortExpression="First_Name" />
            <asp:BoundField DataField="Last_Name" HeaderText="Last_Name" 
                SortExpression="Last_Name" />
            <asp:BoundField DataField="Guarantor_Name" HeaderText="Guarantor_Name" 
                SortExpression="Guarantor_Name" />
            <asp:BoundField DataField="Guarantor_Address" HeaderText="Guarantor Address" 
                SortExpression="Guarantor_Address" />
            <asp:BoundField DataField="Employee_Address" HeaderText="Employee_Address" 
                SortExpression="Employee_Address" />
              <asp:TemplateField HeaderText="Image">
    <itemtemplate>
    <asp:Image ID="Image1" runat="server" 
          ImageUrl='<%# Eval("EmployeeID", "~/ImageHandler.ashx?EmployeeID={0}")%>'/>
    </itemtemplate>
    
        </columns>




This is the code for the httphandler page

VB
<%@ WebHandler Language="VB" Class="ImageHandler" %>

Imports System
Imports System.Web
Imports System.Data.SqlClient

Imports System.Configuration

Imports System.Data

Public Class ImageHandler : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim EmployeeID As Integer = Convert.ToInt32(context.Request.QueryString("EmployeeID"))
        If EmployeeID > 0 Then
            context.Response.BinaryWrite(RetrieveEmployeeImage(EmployeeID))
            context.Response.[End]()
        End If
    End Sub
    Private Function RetrieveEmployeeImage(ByVal EmployeeID As Integer) As [Byte]()
        'fetch the connection string from web.config
        Dim connString As String = ConfigurationManager.ConnectionStrings("LordsBannerMotorsConnectionString1").ConnectionString

        'SQL statement to fetch thumbnail photo
        Dim sql As String = " SELECT * FROM Image WHERE EmployeeID=@EmployeeID"
        

        Dim dtEmployeePhoto As New DataTable()
        'Open SQL Connection
        Using conn As New SqlConnection(connString)
            conn.Open()
            'Initialize command object
            Using cmd As New SqlCommand(sql, conn)
                Dim adapter As New SqlDataAdapter(cmd)
                'Fill the result set
                adapter.Fill(dtEmployeePhoto)
            End Using
        End Using
        Return DirectCast(dtEmployeePhoto.Rows(0)(0), [Byte]())
    
    End Function
    
   
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

  THIS IS THE CODE BEHIND FOR THE PAGE TO DISPLAY THE IMAGE
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Configuration


Partial Class Showemployeeinfo
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub
    Private Sub BindData()
        'Bind the grid view
        GridView1.DataSource = RetrieveImage()
        GridView1.DataBind()
    End Sub
    Private Function RetrieveImage() As DataSet
        'if products is available in the viewstate, use that
        'instead of fetching again from the database
        If ViewState("Image") IsNot Nothing Then
            Return DirectCast(ViewState("Image"), DataSet)
        End If
        'fetch the connection string from web.config
        Dim connectionString As String
        connectionString = ConfigurationManager.ConnectionStrings("LordsBannerMotorsConnectionString1").ConnectionString
        'SQL statement to fetch entries from products
        Dim sql As String = " SELECT * FROM Image "
        Dim dsImage As New DataSet()
        'Open SQL Connection
        Using conn As New SqlConnection(connectionString)
            conn.Open()
            'Initialize command object
            Using cmd As New SqlCommand(sql, conn)
                Dim adapter As New SqlDataAdapter(cmd)
                'Fill the result set
                adapter.Fill(dsImage)
            End Using
        End Using
        ViewState("Image") = dsImage
        Return dsImage
    End Function

    
End Class
Posted
Updated 17-Aug-12 4:22am
v2
Comments
Christian Graus 17-Aug-12 4:44am    
This is a massive code dump. What do the URLs look like in the html ? Is your handler ever called ?
jamiebones 17-Aug-12 4:47am    
yes the handler is called on the gridview.I use an item template.Please can you review the gridview markup.Thanks and God bless you for your time trying to help me out.
Christian Graus 17-Aug-12 5:00am    
If the handler is called and the image is not shown, then it's not returning an image. Is the debugger stepping through and getting the byte stream you expect ? I mean, does it read something more than an empty stream ?

Oh. You're not setting the mime type.

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.contenttype.aspx[^]

You need to do that so the browser knows what you're sending it.


You are right. Dim sql As String = " SELECT * FROM Image WHERE EmployeeID=@EmployeeID" is fine, but you need to also tell your SQL statement what @EmployeeID is, right now you do not. So, you need to read this : http://www.4guysfromrolla.com/webtech/092601-1.shtml but the short version is, you need this : Cmd.Parameters.Add("EmployeeID", EmployeeId). Amusingly, the example on that site, uses the exact parameter name you're needing to add.
 
Share this answer
 
v2
 
Share this answer
 
Comments
Christian Graus 17-Aug-12 5:00am    
Not sure any of this helps. He's doing all those things and needs specific help
codeBegin 17-Aug-12 5:02am    
Please see the first link of my answer. It's accepted answer for similar question (I mean OP did all those things (in that question) which needed by him and asked about specific help still accepted the answer.)

So, why this answer not?
Christian Graus 17-Aug-12 5:05am    
Because he'd already done those things. He needed specific help ( he had not set the MIME type ), not an overarching description of a task he'd already attempted.
jamiebones 17-Aug-12 5:27am    
I think i will go through everything again and start from the scratch.Thanks a lot for all your effort.You too kind
Christian Graus 17-Aug-12 5:29am    
I do think all you need is a MIME type.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900