0

Its my handler (.ashx)

    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" 
  context.Response.BinaryWrite(imageData)

Everything is working fine.Only the imageData length is 0 so the image is unable to display.

@Sean: its elsewhr.. here the querystring correctly takes the employeeid passed...

heres the code for db access:

    Public Sub bind()

    Dim ds1 As New DataSet()
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)
    con.Open()
    Dim query As String = "select * from EmployeeTable"
    Dim cmd As New SqlCommand()
    Dim da1 As New SqlDataAdapter(query, con)
    da1.Fill(ds1, "EmployeeTable")
    GridView1.DataSource = ds1.Tables("EmployeeTable")
    GridView1.DataBind()
    con.Close()
End Sub
who-aditya-nawandar
  • 1,334
  • 9
  • 39
  • 89
  • 2
    There's no code here for the database access... There has to be something wrong with the way you are populating the byte array otherwise it wouldn't have no data. – Sean Airey Mar 05 '13 at 11:46
  • its elsewhr.. here the querystring correctly takes the employeeid passed... heres the code for db access: – who-aditya-nawandar Mar 05 '13 at 11:52
  • Ok so that's not actually putting any data into the `imageData` variable. What data type do you store the image data as in the database? – Sean Airey Mar 05 '13 at 12:01
  • image datatype... (it gets stored as binary data) – who-aditya-nawandar Mar 05 '13 at 12:12
  • can u plz go thru this: http://stackoverflow.com/questions/15198459/displaying-image-from-database-in-an-image-control – who-aditya-nawandar Mar 05 '13 at 12:17
  • Are you having problems getting the handler to work correctly? Try the code below, check that the `imageData` length is no longer 0 and if you have any more problems I'll see if I can help with this other question. – Sean Airey Mar 05 '13 at 12:51

2 Answers2

1

You're loading a load of data into the GridView but nothing is being loaded into your imageData variable. So we'll just connect to the database and pull that data out. I'm assuming your image column is called imageData but please change as appropriate.

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

Using con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))

    Using cmd As New SqlCommand("SELECT imageData FROM EmployeeTable WHERE EmployeeID = @EmployeeID", con) 'select imageData column, change column name as appropriate

        cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID)

        Try

            con.Open()

            Using rdr As SqlDataReader = cmd.ExecuteReader()

                If rdr.Read() Then

                    imageData = CType(rdr("imageData"), Byte()) 'convert imageData column from result set to byte array and assign to variable

                End If

            End Using

        Catch ex As Exception

            'do any error handling here

        End Try

    End Using

End Using

context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(imageData)
Sean Airey
  • 6,352
  • 1
  • 20
  • 38
  • not working... image length 0... n my image column name is Image.. i changed imageData to Image everywhere... – who-aditya-nawandar Mar 06 '13 at 08:59
  • n jst btw, hws gridview concerned in this.. m not trying to display it in a gridview.. i want to display the image in the image control... – who-aditya-nawandar Mar 06 '13 at 09:01
  • the control never goes to rdr.Read() – who-aditya-nawandar Mar 06 '13 at 10:03
  • thnx vvvvvvvv much... if only you wouldnt have forgotten to pass the connection object to the command object, would have saved another hr n half for me... never mind... you helped big time! – who-aditya-nawandar Mar 06 '13 at 10:43
  • Oh I'm really sorry about that, that's a rookie mistake! And sorry for the slow reply I was without internet access all of yesterday but I'm glad you got it sorted in the end =] – Sean Airey Mar 07 '13 at 12:45
  • Oh by the way if someone's answer helps you to solve your problem, you should accept it as the answer which you can do by clicking the green tick to the left of the post. It helps other people who may have a similar problem to see that there is a solution without having to read everything on the page =] – Sean Airey Mar 07 '13 at 12:50
0

Changed the code in the handler to 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"))

    Else

        Throw New ArgumentException("No parameter specified")

    End If

    Dim Image() As Byte = {}

    ' get the image data from the database using the employeeId Querystring

    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)

    Dim cmd As New SqlCommand("SELECT Image FROM EmployeeTable WHERE EmployeeID = @EmployeeID", con)
    'select imageData column, change column name as appropriate

    cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID)

    Try

        con.Open()

        Dim rdr As SqlDataReader = cmd.ExecuteReader()

        While rdr.Read()


            Image = CType(rdr("Image"), Byte())
            'convert imageData column from result set to byte array and assign to variable
        End While

    Catch ex As Exception

        'do any error handling here

    End Try

    context.Response.ContentType = "image/jpeg"
    context.Response.BinaryWrite(Image)

End Sub

Worked for me, may be it will work for you too...

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