Can someone pleaaaase help on this: Image display from SQL Server

G

Guest

Hi,

I'm struggling to display an image on asp.net webform (vb.net). The image is
stored in a image field in a sql server database.

I have read a good few solutions to it and still havnt got it working for
the last 2 days.

Heres my code...

when a user clicks on "View Image" button in a datagrid the following code
executes which just passes the EmployeeID to the webform which displays the
photo of the employee.

Response.Write("<script language = ""Javascript"">var
win=window.open('empphoto.aspx?sid=" & E.Item.Cells(5).Text &
"',null,'width=510,height=355,top=250,left=250','true');</script>")

The empphoto webform has just this in its html..
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Image Width="150" Height="125" ImageUrl='photo.aspx' Runat=server
ID="Image1"/> </form></body>

The photo webform has a page load event of...
Dim sidVar As Integer
sidVar = Request.QueryString("Sid")

Dim dr As SqlDataReader

dr = SQLDAL.SQLDAL.ExecuteDataReader(ConnStr, "HR_GetPhoto")

If dr.Read() Then
Response.Clear()
Response.ContentType = "image/jpeg"
Response.OutputStream.Write(CType(dr("photo"), _
Byte()), 0, CInt(10000))
Response.AddHeader("Content-Disposition", _
"inline;filename=" + dr("photo").ToString())

End If

Its really this last piece of code that I think is the problem. Can anyone
help? Either point me in the direction of some code that does work or can you
see whats wrong in my code? All I get is the horrible red x image.

Thanks
N
 
C

Craig Deelsnyder

NH said:
Hi,

I'm struggling to display an image on asp.net webform (vb.net). The image is
stored in a image field in a sql server database.

I have read a good few solutions to it and still havnt got it working for
the last 2 days.

Heres my code...

when a user clicks on "View Image" button in a datagrid the following code
executes which just passes the EmployeeID to the webform which displays the
photo of the employee.

Response.Write("<script language = ""Javascript"">var
win=window.open('empphoto.aspx?sid=" & E.Item.Cells(5).Text &
"',null,'width=510,height=355,top=250,left=250','true');</script>")

The empphoto webform has just this in its html..
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Image Width="150" Height="125" ImageUrl='photo.aspx' Runat=server
ID="Image1"/> </form></body>

The photo webform has a page load event of...
Dim sidVar As Integer
sidVar = Request.QueryString("Sid")

Dim dr As SqlDataReader

dr = SQLDAL.SQLDAL.ExecuteDataReader(ConnStr, "HR_GetPhoto")

If dr.Read() Then
Response.Clear()
Response.ContentType = "image/jpeg"
Response.OutputStream.Write(CType(dr("photo"), _
Byte()), 0, CInt(10000))
Response.AddHeader("Content-Disposition", _
"inline;filename=" + dr("photo").ToString())

End If

Its really this last piece of code that I think is the problem. Can anyone
help? Either point me in the direction of some code that does work or can you
see whats wrong in my code? All I get is the horrible red x image.

Thanks
N

Make sure photo.aspx has absolutely no HTML/content in its .aspx file.
Only scripts if needed...My first guess (and thing to check) is that
there is some text, etc. in the .aspx that is getting output as text
with the binary data (a no-no). Only one content-type per request.

So go into your .aspx and make sure all whitespace, HTML, everything but
the directives and server-side scripts are removed (even the CRLF after
the directives)...

If you still have a problem, have you debugged to make sure an exception
is not getting thrown inside your code?

Here's the only example I could find right now, which reads the image
from a file, you can easily adapt to use your byte array:

http://authors.aspalliance.com/stevesmith/articles/displayimage.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top