Link to home
Start Free TrialLog in
Avatar of sparilla
sparilla

asked on

How do I construct a Image Handler for Multiple Images in a Database?

I am trying to see my uploaded images but I can only see one when I use my handler. I tried adding ("Image, Image2") into the Handler, but I received an error. I have truly exhausted all my time, and I need help. Im sure I am not following the rules regarding my format, so I need some help.

By the way, when the page is launched I can see images only in the "Image" column.

Thank you very much,
Sparilla
'Here is the Aspx Page I am Uploading with
 
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 
<script runat="server">
 
    Protected Sub dlstImages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
 
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Photo Gallery</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:DataList
        id="dlstImages"
        DataSourceID="srcImages"
        RepeatColumns="3"
        Runat="server" onselectedindexchanged="dlstImages_SelectedIndexChanged" 
            BorderColor="Black" BorderWidth="1px">
        <ItemTemplate>
        <asp:Image ID="Image1"
            ImageUrl='<%# String.Format("DynamicImage.ashx?id={0}", Eval("Id")) %>'
            Width="250"
            Runat="server" />
        <br />
        <%# Eval("Description") %>
    </ItemTemplate>
    </asp:DataList>
         
    <hr />
 
    <asp:FormView
        id="frmImage"
        DataSourceID="srcImages"
        DefaultMode="Insert"
        Runat="server">
        <InsertItemTemplate>
        <asp:Label
            id="lblImage"
            Text="Upload Image:"
            AssociatedControlId="upImage"
            Runat="server" />
        <br />    
        <asp:FileUpload
            id="upImage"
            FileBytes='<%# Bind("Image") %>'
            Runat="server" />
        
        <br /><br />
        
        <asp:Label
            id="lblDescription"
            Text="Description:"
            AssociatedControlID="txtDescription"
            Runat="server" />
        <br />    
        <asp:TextBox
            id="txtDescription"
            Text='<%# Bind("Description") %>'
            TextMode="MultiLine"
            Columns="50"
            Rows="2"
            Runat="server" />    
        
        <br /><br />
        
        <asp:Button
            id="btnInsert"
            Text="Add Image"
            CommandName="Insert"
            Runat="server" />    
        </InsertItemTemplate>
    </asp:FormView>    
        
   
    
    <asp:SqlDataSource
        id="srcImages"
        SelectCommand="SELECT ID,Description FROM Images"
        InsertCommand="INSERT Images (Image,Description)
            VALUES (@Image,@Description)"
        ConnectionString="<%$ ConnectionStrings:Images %>"
        Runat="server" />
    
    
        <br />
        <br />
    
    
    </div>
    </form>
</body>
</html>
 
 
 
 
 
 
'This is the Handler 
 
 
<%@ WebHandler Language="VB" Class="DynamicImage" %>
 
Imports System.Data
Imports System.Web
Imports System.Web.Configuration
Imports System.Web.UI
Imports System.Web.UI.WebControls
 
''' <summary>
''' Displays an image corresponding to the Id passed
''' in a query string field
''' </summary>
Public Class DynamicImage
	 Implements IHttpHandler
 
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        ' Get the Id of the image to display
        Dim imageId As String = context.Request.QueryString("Id")
 
        ' Use SqlDataSource to grab image bytes
        Dim src As SqlDataSource = New SqlDataSource()
        src.ConnectionString = WebConfigurationManager.ConnectionStrings("Images").ConnectionString
        src.SelectCommand = "SELECT Image FROM Images WHERE Id=" + imageId
        
       
 
        ' Return a DataView
        Dim view As DataView = CType(src.Select(DataSourceSelectArguments.Empty), DataView)
        context.Response.BinaryWrite(CType(view(0)("Image"), Byte()))
 
        ' Return a DataReader
        'src.DataSourceMode = SqlDataSourceMode.DataReader
        'Dim reader As IDataReader = CType(src.Select(DataSourceSelectArguments.Empty), IDataReader)
        'reader.Read()
        'context.Response.BinaryWrite(CType(reader("Image"), Byte()))
        'reader.Close()
 
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
 
End Class
 
 
 
'Here is the database
 
Id              Int               Dont Allow Nulls
Image          varbinary(MAX)    Allowed Nulls
Description     nvarchar(MAX)    Allowed Nulls
Image2          varbinary(MAX)    Allowed Nulls
Description2     nvarchar(MAX)    Allowed Nulls
Image3          varbinary(MAX)    Allowed Nulls
Description3     nvarchar(MAX)    Allowed Nulls

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

When you processed multiple images, did you create a loop block to repeat the steps multiple times?

Bob
Avatar of sparilla
sparilla

ASKER

No i didnt. Do you have an example of how that could be done Bob?
Where you looking to pass multiple IDs in the QueryString values?

Default.aspx?id=1720, 310, 1312

Bob
I wanted to pass all IDs if possible, but it's not the ID im having a problem with. I can only get the first image to show, and the second one will not.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This worked out perfectly.