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

Discussion in 'ASP .Net' started by =?Utf-8?B?Tkg=?=, Mar 24, 2005.

  1. 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
     
    =?Utf-8?B?Tkg=?=, Mar 24, 2005
    #1
    1. Advertising

  2. NH wrote:
    > 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

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Mar 24, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YQ==?=
    Replies:
    3
    Views:
    17,950
    Carlos Barini
    Jun 7, 2004
  2. TN Bella
    Replies:
    3
    Views:
    475
    TN Bella
    Jun 22, 2004
  3. George
    Replies:
    1
    Views:
    312
  4. ecoolone
    Replies:
    0
    Views:
    765
    ecoolone
    Jan 3, 2008
  5. JP

    Display image from sql server

    JP, Aug 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    328
    Ken Varn
    Aug 15, 2005
Loading...

Share This Page