Click here to Skip to main content
15,904,297 members
Home / Discussions / JavaScript
   

JavaScript

 
Questionretrieve and display image from SQL server in html Pin
Member 1057920214-Feb-14 11:18
Member 1057920214-Feb-14 11:18 
AnswerRe: retrieve and display image from SQL server in html Pin
Richard Deeming17-Feb-14 2:04
mveRichard Deeming17-Feb-14 2:04 
GeneralRe: retrieve and display image from SQL server in html Pin
Member 1057920218-Feb-14 7:46
Member 1057920218-Feb-14 7:46 
GeneralRe: retrieve and display image from SQL server in html Pin
Richard Deeming18-Feb-14 8:04
mveRichard Deeming18-Feb-14 8:04 
GeneralRe: retrieve and display image from SQL server in html Pin
Member 1057920218-Feb-14 8:09
Member 1057920218-Feb-14 8:09 
GeneralRe: retrieve and display image from SQL server in html Pin
Richard Deeming18-Feb-14 8:14
mveRichard Deeming18-Feb-14 8:14 
GeneralRe: retrieve and display image from SQL server in html Pin
Member 1057920218-Feb-14 8:41
Member 1057920218-Feb-14 8:41 
GeneralRe: retrieve and display image from SQL server in html Pin
Richard Deeming18-Feb-14 8:59
mveRichard Deeming18-Feb-14 8:59 
OK, I see the problem. You're calling Response.Write, which accepts a String, but you're passing an array of Bytes. You need to call Response.BinaryWrite instead. If you have Option Strict turned on, you'll need to convert the Object to a Byte() first:
VB
Dim imageBytes As Byte() = DirectCast(sdr(2), Byte())
context.Response.BinaryWrite(imageBytes)

Also:
  • You should wrap the SqlDataReader in a Using block.
  • You don't need to check sdr.HasRows before calling the sdr.Read() method.
  • Since you're only returning one record, you can replace the While sdr.Read() ... End While block with an If sdr.Read() Then ... End If block.
  • You should probably set the status code to 404 if you don't find an image to return.
  • I'm not sure what you're doing with the ArrayList variable; you seem to store things in it, but never use it. You also have an unused DataSet variable at the top of your method.


The tidied-up version of your code is:
VB
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Using con As New SqlConnection(GetConnectionString("myconnection"))
        Using cmd As New SqlCommand()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "spRFIDocumentByRequestIDSelect"
            cmd.Parameters.AddWithValue("@pRequestID", 531)
            cmd.Connection = con
            
            con.Open()
            
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                If srd.Read()
                    Dim imageBytes As Byte() = DirectCast(sdr(2), Byte())
                    
                    context.Response.ContentType = "image/jpeg"
                    context.Response.BinaryWrite(imageBytes)
                Else
                    context.Response.StatusCode = 404
                End If
            End Using
        End Using
    End Using
End Sub

Once your handler is returning the image properly, you can replace your original javascript function with a simple <img> tag as per my first answer. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: retrieve and display image from SQL server in html Pin
Member 1057920218-Feb-14 9:04
Member 1057920218-Feb-14 9:04 
QuestionHow to set color in whole project Pin
Member 1059282712-Feb-14 20:01
Member 1059282712-Feb-14 20:01 
AnswerRe: How to set color in whole project Pin
Richard Deeming13-Feb-14 1:26
mveRichard Deeming13-Feb-14 1:26 
QuestionAdblock system Pin
Member 804117812-Feb-14 18:23
Member 804117812-Feb-14 18:23 
AnswerRe: Adblock system Pin
Richard MacCutchan12-Feb-14 21:46
mveRichard MacCutchan12-Feb-14 21:46 
AnswerRe: Adblock system Pin
Sibeesh KV29-Sep-14 2:02
professionalSibeesh KV29-Sep-14 2:02 
Questioncopy items of one dropdownlist to another one with jquery Pin
H.Goli11-Feb-14 6:38
H.Goli11-Feb-14 6:38 
AnswerRe: copy items of one dropdownlist to another one with jquery Pin
Kornfeld Eliyahu Peter11-Feb-14 6:54
professionalKornfeld Eliyahu Peter11-Feb-14 6:54 
QuestionCreating a static Map Pin
mrkeivan7-Feb-14 19:34
mrkeivan7-Feb-14 19:34 
GeneralRe: Creating a static Map Pin
Sunasara Imdadhusen21-May-14 23:43
professionalSunasara Imdadhusen21-May-14 23:43 
AnswerRe: Creating a static Map Pin
Sibeesh KV29-Sep-14 2:04
professionalSibeesh KV29-Sep-14 2:04 
QuestionBad Request Call WCF Service Jquery Ajax Pin
Ericsson Alves6-Feb-14 15:32
Ericsson Alves6-Feb-14 15:32 
QuestionSolve datetime country wise to display? Pin
pkarthionline6-Feb-14 0:46
pkarthionline6-Feb-14 0:46 
AnswerRe: Solve datetime country wise to display? Pin
Garth J Lancaster6-Feb-14 0:55
professionalGarth J Lancaster6-Feb-14 0:55 
AnswerRe: Solve datetime country wise to display? Pin
Richard Deeming6-Feb-14 2:08
mveRichard Deeming6-Feb-14 2:08 
QuestionSyntax error on IE10 but working fine on IE 8... Pin
Member 102794055-Feb-14 19:57
Member 102794055-Feb-14 19:57 
AnswerRe: Syntax error on IE10 but working fine on IE 8... Pin
Richard Deeming6-Feb-14 2:05
mveRichard Deeming6-Feb-14 2:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.