ASP Displaying Images from within SQL DB

Discussion in 'ASP General' started by Sp33di3, Oct 27, 2003.

  1. Sp33di3

    Sp33di3 Guest

    I've been searching and searching for this and there are lots of posts - but
    very few answers.

    I have a SQL2000 Database that has jpg images in a table that I want to
    display in a webpage. I can return all the other information in the
    database except the pictures. I can't take the images out and link them. I
    have 2 different results. The code included below gives me a page that says
    "This is a picture:" and then box with a red X.

    If I change it around I can get a very long page filled with nothing but
    jumbled characters.

    Can someone please point me to a resolution

    Thanks



    *****MAIN.ASP********
    DIM objConn
    Dim objRS
    Dim objCommand
    Set objConn = Server.CreateObject("ADODB.Connection")
    Set objRS = Server.CreateObject("ADODB.Recordset")
    Set objCommand = Server.CreateObject("ADODB.Command")
    'connection string to SQL Database
    objConn.Open "Provider=SQLOLEDB.1;Password=XXXXXXXX;Persist Security
    Info=True;User ID=sa;Initial Catalog=test;Data Source=XXXXXXXX"

    objCommand.ActiveConnection = objConn
    objCommand.CommandText = "SELECT * FROM Sheet1 WHERE Surname LIKE 'Blythe'"
    objCommand.CommandType=adCmdText
    Set objRS = objCommand.Execute

    if objRS.EOF = False Then
    While not objRS.EOF
    Response.Write objRS("First") & " " & objRS("Surname")& " " & objRS("DOB")
    & " " & "<BR>"
    objRS.MoveNext
    Wend

    Else
    Response.Write "No Records Found"
    end if
    %>
    This is a Picture:

    <IMG alt="" src="ShowPicture.asp">
    <%
    'close DB Connection and Clean up.
    objRS.Close
    objConn.Close
    Set objCommand = Nothing
    Set objRS = Nothing
    Set objConn = Nothing
    %>
    </Body>
    </html>



    **************ShowPicture.asp************
    FILE="C:\Program Files\Common Files\SYSTEM\ADO\msado15.dll" -->


    DIM objConn
    Dim objRS
    Dim objCommand
    Set objConn = Server.CreateObject("ADODB.Connection")
    Set objRS = Server.CreateObject("ADODB.Recordset")
    Set objCommand = Server.CreateObject("ADODB.Command")
    'connection string to SQL Database
    objConn.Open "Provider=SQLOLEDB.1;Password=XXXXXXXX;Persist Security
    Info=True;User ID=sa;Initial Catalog=test;Data Source=XXXXXXXX"

    objCommand.ActiveConnection = objConn
    objCommand.CommandText = "SELECT picture FROM Sheet1 WHERE Surname LIKE
    'Blythe'"
    objCommand.CommandType=adCmdText
    Set objRS = objCommand.Execute

    if objRS.EOF = False Then
    Response.ContentType = "image/jpeg"
    Response.BinaryWrite objRS("picture")
    Else
    Response.Write "No Records Found"
    end if

    'close DB Connection and Clean up.
    objRS.Close
    objConn.Close
    Set objCommand = Nothing
    Set objRS = Nothing
    Set objConn = Nothing
     
    Sp33di3, Oct 27, 2003
    #1
    1. Advertising

  2. Sp33di3

    CJM Guest

    To be honest I'm not sure how well SQL Server handles BLOBs, but regardless,
    I would recommend a different solution:

    Store your JPG's in a folder under the web server, and store the path to the
    JPG in the DB. Then simple point to the pic from within your ASP:
    <IMG alt="" src="<%=objRS("PicturePath"%>">

    It will make your code simpler, slicker and more maintainable...

    hth

    Chris

    "Sp33di3" <> wrote in message
    news:...
    > I've been searching and searching for this and there are lots of posts -

    but
    > very few answers.
    >
    > I have a SQL2000 Database that has jpg images in a table that I want to
    > display in a webpage. I can return all the other information in the
    > database except the pictures. I can't take the images out and link them.

    I
    > have 2 different results. The code included below gives me a page that

    says
    > "This is a picture:" and then box with a red X.
    >
    > If I change it around I can get a very long page filled with nothing but
    > jumbled characters.
    >
    > Can someone please point me to a resolution
    >
    > Thanks
    >
    >
    >
    > *****MAIN.ASP********
    > DIM objConn
    > Dim objRS
    > Dim objCommand
    > Set objConn = Server.CreateObject("ADODB.Connection")
    > Set objRS = Server.CreateObject("ADODB.Recordset")
    > Set objCommand = Server.CreateObject("ADODB.Command")
    > 'connection string to SQL Database
    > objConn.Open "Provider=SQLOLEDB.1;Password=XXXXXXXX;Persist Security
    > Info=True;User ID=sa;Initial Catalog=test;Data Source=XXXXXXXX"
    >
    > objCommand.ActiveConnection = objConn
    > objCommand.CommandText = "SELECT * FROM Sheet1 WHERE Surname LIKE

    'Blythe'"
    > objCommand.CommandType=adCmdText
    > Set objRS = objCommand.Execute
    >
    > if objRS.EOF = False Then
    > While not objRS.EOF
    > Response.Write objRS("First") & " " & objRS("Surname")& " " &

    objRS("DOB")
    > & " " & "<BR>"
    > objRS.MoveNext
    > Wend
    >
    > Else
    > Response.Write "No Records Found"
    > end if
    > %>
    > This is a Picture:
    >
    > <IMG alt="" src="ShowPicture.asp">
    > <%
    > 'close DB Connection and Clean up.
    > objRS.Close
    > objConn.Close
    > Set objCommand = Nothing
    > Set objRS = Nothing
    > Set objConn = Nothing
    > %>
    > </Body>
    > </html>
    >
    >
    >
    > **************ShowPicture.asp************
    > FILE="C:\Program Files\Common Files\SYSTEM\ADO\msado15.dll" -->
    >
    >
    > DIM objConn
    > Dim objRS
    > Dim objCommand
    > Set objConn = Server.CreateObject("ADODB.Connection")
    > Set objRS = Server.CreateObject("ADODB.Recordset")
    > Set objCommand = Server.CreateObject("ADODB.Command")
    > 'connection string to SQL Database
    > objConn.Open "Provider=SQLOLEDB.1;Password=XXXXXXXX;Persist Security
    > Info=True;User ID=sa;Initial Catalog=test;Data Source=XXXXXXXX"
    >
    > objCommand.ActiveConnection = objConn
    > objCommand.CommandText = "SELECT picture FROM Sheet1 WHERE Surname LIKE
    > 'Blythe'"
    > objCommand.CommandType=adCmdText
    > Set objRS = objCommand.Execute
    >
    > if objRS.EOF = False Then
    > Response.ContentType = "image/jpeg"
    > Response.BinaryWrite objRS("picture")
    > Else
    > Response.Write "No Records Found"
    > end if
    >
    > 'close DB Connection and Clean up.
    > objRS.Close
    > objConn.Close
    > Set objCommand = Nothing
    > Set objRS = Nothing
    > Set objConn = Nothing
    >
    >
     
    CJM, Oct 28, 2003
    #2
    1. Advertising

  3. Sp33di3

    Sp33di3 Guest

    I said in my original post that I can't take the images out of the DB.



    "CJM" <> wrote in message
    news:%...
    > To be honest I'm not sure how well SQL Server handles BLOBs, but

    regardless,
    > I would recommend a different solution:
    >
    > Store your JPG's in a folder under the web server, and store the path to

    the
    > JPG in the DB. Then simple point to the pic from within your ASP:
    > <IMG alt="" src="<%=objRS("PicturePath"%>">
    >
    > It will make your code simpler, slicker and more maintainable...
    >
    > hth
    >
    > Chris
    >
     
    Sp33di3, Oct 28, 2003
    #3
  4. Sp33di3

    CJM Guest

    "Sp33di3" <> wrote in message
    news:...
    > I said in my original post that I can't take the images out of the DB.
    >

    Apologies... I scanned over the post too quickly!

    But I'm curious as to why you can't? I assume it is a constraint
    someone\something else is placing on you?

    I'm afraid I can't really be any more help; I've never used BLOBs to store
    images

    Good Luck.

    Chris
     
    CJM, Oct 28, 2003
    #4
    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. Ken Varn
    Replies:
    2
    Views:
    578
    Ken Varn
    May 11, 2004
  2. buzz
    Replies:
    1
    Views:
    1,153
    =?Utf-8?B?Q3JhaWc=?=
    Aug 31, 2005
  3. =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    RE: ASP.NET website NOT displaying any images - HELP!!

    =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=, Dec 1, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    675
    =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=
    Dec 1, 2006
  4. Just Me
    Replies:
    2
    Views:
    543
    John Mott
    Sep 4, 2007
  5. whyman
    Replies:
    4
    Views:
    972
    whyman
    Mar 23, 2006
Loading...

Share This Page