[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

How do I use a handler to see multiple Images that are under the same ID?

This is different from a previous one where i needed to 1 picture under one Id. Now I need to see multiple pictures under one Id. I have the upload part right, but I need the right handler. Thank you very much, for spending time helping me.

Sincerely,
Alex
Here is the aspx page.
 
<%@ 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:Label
            id="Label1"
            Text="Upload Image:"
            AssociatedControlId="upImage"
            Runat="server" />
        <br />    
        <asp:FileUpload
            id="FileUpload1"
            FileBytes='<%# Bind("Image2") %>'
            Runat="server" />
        
          <br /><br />
           <asp:FileUpload
            id="FileUpload2"
            FileBytes='<%# Bind("Image3") %>'
            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,Image2,Image3,Description)
            VALUES (@Image,@Image2,@Image3,@Description)"
        ConnectionString="<%$ ConnectionStrings:Images %>"
        Runat="server" />
    
    
        <br />
        <br />
    
    
    </div>
    </form>
</body>
</html>
 
 
                           Here is the Handler
 
 
 
<%@ WebHandler Language="VB" Class="DynamicImage" %>
 
Imports System.Data
Imports System.Web.Configuration
 
''' <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
        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)
     
            
                src.SelectCommand = "Select Image FROM Images Where Id=" + imageIdList.Trim()
            
                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
 
 
 
                          Here is what the database looks like
 
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
sparilla
Asked:
sparilla
  • 11
  • 8
1 Solution
 
Bob LearnedCommented:
I don't see how this is different than your other question:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23137774.html

Bob
0
 
sparillaAuthor Commented:
It is different because the other handler wanted to display an image1 from the images database, this new one needs to display image1 and image2. Both binary sets need to be represented with the same Id.

The aspx page has an extra upload going to the same id
0
 
Bob LearnedCommented:
Does that mean that a single ID will retrieve multiple images for the same ID?

If that is true, then you should be able to loop through each DataRowView in the DataView:
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)
     
            
                src.SelectCommand = "Select Image FROM Images Where Id=" + imageIdList.Trim()
            
                Dim view As DataView = CType(src.Select(DataSourceSelectArguments.Empty), DataView)
                For Each drv As DataRowView In view
                    context.Response.BinaryWrite(CType(drv("Image"), Byte()))
                Next drv
                           
            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
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.

 
sparillaAuthor Commented:
Is that the same code as last time?
0
 
sparillaAuthor Commented:
Im just curious on how does it know to get image2 from the same ID if Image2 isnt in the handler.

ex: src.SelectCommand = "Select Image FROM Images Where Id=" + imageIdList.Trim()

You wouldnt have to say something like:
src.SelectCommand = "Select (Image,Image2) FROM Images Where Id=" + imageIdList.Trim()

???

0
 
Bob LearnedCommented:
This is different:

              Dim view As DataView = CType(src.Select(DataSourceSelectArguments.Empty), DataView)
                For Each drv As DataRowView In view
                    context.Response.BinaryWrite(CType(drv("Image"), Byte()))
                Next drv

There would be 2 image records in the database for the same image ID, with this SQL:

src.SelectCommand = "Select Image FROM Images Where Id=" + imageIdList.Trim()

Now, if you have multiple image columns (Image1, Image2), then we're talking something different.

Bob
0
 
sparillaAuthor Commented:
It still only shows 1 image out of two when the ID is queried to the handler. Is it possible to speciify it to look it up by key and then in image and image2?
0
 
sparillaAuthor Commented:
"Now, if you have multiple image columns (Image1, Image2), then we're talking something different."

That is exactly what I need.

-Alex
0
 
Bob LearnedCommented:
Here is what I would try:


src.SelectCommand = "Select Image, Image2 FROM Images Where Id=" + imageIdList.Trim()
            
Dim view As DataView = CType(src.Select(DataSourceSelectArguments.Empty), DataView)
 
If view.Count > 0 Then
    context.Response.BinaryWrite(CType(view(0)("Image"), Byte()))
    context.Response.BinaryWrite(CType(view(0)("Image2"), Byte()))
End If

Open in new window

0
 
sparillaAuthor Commented:
This is the error i got using the code. It showed image(otherwise known as the first image under the first Id), but not image2 (the second column)
error-thrown.JPG
0
 
Bob LearnedCommented:
That sounds like Image2 is not a valid binary stream.

Bob
0
 
sparillaAuthor Commented:
how do i fix that?
0
 
sparillaAuthor Commented:
If you get that code to work for you, could you send the files in a .zip format? I want to know if that can be done soo bad.
0
 
Bob LearnedCommented:
You should be storing images in the same column type, in the same way.  Then, you wouldn't be getting that exception.  I am not sure if looking at your code will help me understand why it is happening.

Bob
0
 
sparillaAuthor Commented:
Heres the entire file im working with
http://download.yousendit.com/ECA2872A41F28BD4 
0
 
sparillaAuthor Commented:
Should i use a property like SourceColumn for mapping?
0
 
Bob LearnedCommented:
1) I can't work with .rar files, and I don't really want to install anything to do so.

2) I don't think that any file download is necessary.

3) You should be examining the process that you are using to store images into the 2 fields.

4) If some of the rows don't have 2 images, then you need to check for that when you are getting the data from the row.

Bob
0
 
sparillaAuthor Commented:
I honestly dont know how. Do you use .zip? Rar is the same thing but i can zip it using .zip.
0
 
Bob LearnedCommented:
I was trying to say that I really don't have time to look at anything that you would attach to this question, but I do have time enough for 5 minutes worth of coaching advise to help you find a solution.

Bob
0

Featured Post

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.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now