Thursday, December 17, 2009

Retrieving a Image from database and visualizing in Gridview Image based on the primary key column

Gridview
---------------------



ImageUrl='<%#"StaffHandler.ashx?id=" & Eval("staffid")%>'
idth="171px" Height="200px" />





-----------------------------
StaffHandler.ashx
-----------------------------

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

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

Public Class Handler : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Try
Dim myConnection As New SqlConnection
Call DBConnect(myConnection)

Dim sql As String = "Select photo from staff_details where staffid=@staffid"

Dim cmd As New SqlCommand(sql, myConnection)
cmd.Parameters.Add("@staffid", System.Data.SqlDbType.VarChar, 50).Value = context.Request.QueryString("id")
cmd.Prepare()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
context.Response.BinaryWrite(DirectCast(dr("photo"), Byte()))
Catch
End Try
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class

'''''''''''''''''''''''''''''''''''''''''
In Button CLick or timer_tick
'''''''''''''''''''''''''''''''''''''''''

Dim sql As String = "Select staffid,photo from staff_details where staffid='" & ds.Tables(0).Rows(count)(0).ToString() & "'"

Dim ada As New SqlDataAdapter(sql, con)
Dim dt As New DataTable()
ada.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()

'''''''''''''''''''''''''''''

No comments:

Post a Comment