?
Solved

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

Posted on 2008-02-05
6
Medium Priority
?
710 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:sparilla
  • 3
  • 3
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20822904
When you processed multiple images, did you create a loop block to repeat the steps multiple times?

Bob
0
 
LVL 1

Author Comment

by:sparilla
ID: 20823905
No i didnt. Do you have an example of how that could be done Bob?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20824035
Where you looking to pass multiple IDs in the QueryString values?

Default.aspx?id=1720, 310, 1312

Bob
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 1

Author Comment

by:sparilla
ID: 20824934
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20824999
This is what I would try:
Imports System.Data
Imports System.Web.Configuration
 
Public Class DynamicImage
  Implements IHttpHandler
 
  Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
 
    ' Get the Id of the image to display
    If context.Request.QueryString("id") IsNot Nothing Then
      Dim imageIdList As String = context.Request.QueryString("Id")
 
      Dim src As New SqlDataSource()
      src.ConnectionString = WebConfigurationManager.ConnectionStrings("Images").ConnectionString
 
      For Each imageId As String In imageIdList.Split(","c)
 
        ' Use SqlDataSource to grab image bytes
        src.SelectCommand = "SELECT Image FROM Images WHERE Id=" + imageId.Trim()
 
        ' Return a DataView
        Dim view As DataView = CType(src.Select(DataSourceSelectArguments.Empty), DataView)
 
        If view.Count > 0 Then
          context.Response.BinaryWrite(CType(view(0)("Image"), Byte()))
        End If
 
      Next imageId
 
    End If
 
  End Sub
 
  Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
      Return False
    End Get
  End Property
 
End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:sparilla
ID: 31428080
This worked out perfectly.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question