0

Triyng to retrieve and display image from database in an image control based on employeeid... I have taken an httphandler in which i have this:

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

    'context.Response.ContentType = "text/plain"
    'context.Response.Write("Hello World!")

    Dim employeeId As Integer
    If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
        employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
    Else
        Throw New ArgumentException("No parameter specified")
    End If
    Dim imageData() As Byte = {}
    ' get the image data from the database using the employeeId Querystring
    context.Response.ContentType = "image/jpeg"
    ' You can retrieve this also from the database
    context.Response.BinaryWrite(imageData)

End Sub

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click
    bind()
    GridView1.Visible = "True"
    ProcessRequest(Context)
End Sub

Error: The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event. Where am I going wrong? What all changes do i need to make?

This is the image control on the form:

<asp:Image ID="Image1" runat="server" imageUrl="HttpHandler.ashx?employeeId=5"/>

@Stefano Altieri:

This is on Employee.aspx

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click
    bind()
    GridView1.Visible = "True"
    Image1.ImageUrl = "~/HttpHandler.ashx?EmployeeID='" & EmailIDTextBox.Text & "'"
End Sub

and this is on HttpHandler.ashx

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

    'context.Response.ContentType = "text/plain"
    'context.Response.Write("Hello World!")

    Dim employeeId As Integer
    If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
        employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
    Else
        Throw New ArgumentException("No parameter specified")
    End If
    Dim imageData() As Byte = {}
    ' get the image data from the database using the employeeId Querystring
    context.Response.ContentType = "image/jpeg"
    ' You can retrieve this also from the database
    context.Response.BinaryWrite(imageData)

End Sub
who-aditya-nawandar
  • 1,334
  • 9
  • 39
  • 89

2 Answers2

0

Generic Handler May helps you..

0

your handler should be like this

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim employeeId As Integer
If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
Dim con As SqlConnection("your connection string")    
Dim cmd As SqlCommand("select image_colum from <table_name> where employeeID = "+employeeID)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim picture As Byte() = dr[0]
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(picture)
End If
End Sub
Ravinder Gujiri
  • 1,494
  • 10
  • 14