Results 1 to 2 of 2

Thread: Problem bringing back images from database

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    50

    Problem bringing back images from database

    Hi all:

    I'm currently working on a project and basically have come to a standstill, and was wondering if you could help me out. Most of this requires knowledge of ASP.net and VB.net. I'm attempting to dynamically retrieve multiple images from a SQL database and display it onto an ASP (.aspx) page depending upon what the user selects. Currently I have a .ASHX page bringing back the images, but right now, it's just displaying the very first image. I'm trying to get it to retrieve ALL the images that the SELECT statement brings back, and display all of them. I figured if I can get the very first one to display, I should be able to get the rest of them to display as well.

    Here's the code. Please bear in mind this is a condensed verison:

    .ASHX page

    <%@ WebHandler Language="VB" Class="FileHandler" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SQLClient" %>
    <%@ import Namespace="System.IO" %>
    <%@ import Namespace="System.Web.UI.WebControls" %>
    <%@ import Namespace="System.Web.UI.HtmlControls" %>
    <%@ import Namespace="System.Web.Mail" %>

    Imports System
    Imports System.Web
    Imports System.Data
    Imports System.Data.SqlClient

    Public Class FileHandler
    Implements IHttpHandler

    Const conString As String = "server/db"

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest


    Dim con As SqlConnection = New SqlConnection(conString)

    Dim cmd As SqlCommand = New SqlCommand("SELECT FileImage, FileType, filename FROM Attachments WHERE ArticleId=@ArticleId", con)
    cmd.Parameters.AddWithValue("@ArticleId", context.Request("ArticleId"))


    Using con
    con.Open()

    Dim ds As New DataSet
    Dim da As New SqlDataAdapter(cmd, con)
    Dim arrContent As Byte()
    Dim dr As DataRow

    da.Fill(ds)
    dr = ds.Tables(0).Rows(Convert.ToInt32(Request.QueryString("ImageIndex"))).Item("FileImage")
    arrContent = CType(dr.Item("FileImage"), Byte())
    Dim conType As String = dr.Item("FileType").ToString()
    Response.ContentType = conType
    Response.OutputStream.Write(arrContent, 0, dr.Item("filename"))
    Response.End()

    con.Close()
    End Using

    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
    Return False
    End Get
    End Property

    End Class

    And then I have the .aspx, which is supposed to display the images

    <%@ Page Language="VB" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SQLClient" %>
    <%@ import Namespace="System.IO" %>
    <%@ import Namespace="System.Web.UI.WebControls" %>
    <%@ import Namespace="System.Web.UI.HtmlControls" %>
    <%@ import Namespace="System.Web.Mail" %>

    <script language=vbscript runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not Page.IsPostBack Then

    Dim connStr As String = "server/db"
    Dim DBReader As SqlDataReader
    Dim Article As String = Request.QueryString("ArticleID")
    Dim DBConnection As SqlConnection = New SqlConnection(connStr)
    Dim MySQL As String = "Select * from Vw_Article Where ArticleID = " & Article
    Dim DBCommand As New SqlCommand(MySQL, DBConnection)

    DBConnection.Open()

    DBReader = DBCommand.ExecuteReader()
    DetailList.DataSource = DBReader
    DetailList.DataBind()
    DBReader.Close()
    DBConnection.Close()


    End If
    End Sub

    <asp:Image ID="image1" runat="server" ImageUrl='<%#Eval("Articleid", "documenthandler-test.ashx?Articleid={0}&ImageIndex=0")%>'/>
    <asp:Image ID="image2" runat="server" ImageUrl='<%#Eval("Articleid", "documenthandler-test.ashx?Articleid={0}&ImageIndex=1")%>'/>
    <asp:Image ID="image3" runat="server" ImageUrl='<%#Eval("Articleid", "documenthandler-test.ashx?Articleid={0}&ImageIndex=2")%>'/>
    <asp:Image ID="image4" runat="server" ImageUrl='<%#Eval("Articleid", "documenthandler-test.ashx?Articleid={0}&ImageIndex=3")%>'/>

    The ImageIndex is what supposed to bring back each individual image, for this particular ArticleID, there's four images.

    Could someone please take a look at this code and tell me what I'm doing wrong. I need this for work.

    Thanks,
    -Wes
    Last edited by webwiz082; Apr 9th, 2007 at 01:49 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Problem bringing back images from database

    Compile your project, run it. Then, try accessing the page directly, by browsing directly to:

    http://localhost/something/documenth...1&ImageIndex=3

    Make sure there's a breakpoint at

    da.Fill(ds)

    Also ensure that you change

    Code:
    SELECT FileImage, FileType, filename FROM Attachments WHERE ArticleId=@ArticleId
    to
    Code:
    SELECT ArticleId, FileImage, FileType, filename FROM Attachments WHERE ArticleId=@ArticleId
    Also, have it return AttachmentsId? Surely there's a primary key of some sort in that table?

    Once that line executes (F10), do a quickwatch on ds, and try to see what value is returned in AttachmentsId/ArticleId along with the other fields. You must be able to determine whether the same row is being repeated 4 times. There should be at least 4 different filename fields, right?

    Now, try browsing to the same page with ImageIndex = 0, then 1, then 2, then 3. This stepping through as well as direct browsing should help you narrow down the problem, as to why the same image appears 4 times. Might even be a caching issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width