Search code examples
c#asp.netsql-serverrepeatersqldatasource

asp.net c# Repeating only 1 of 3 images related to record


i want to repeat out all items from a single category (selected via querystring) which currently works.

However, each item is currently shown (repeated) 3 times, all 3 with the same text just a different image, because each item has 3 different images associated with it.

On this page, i only want to display one of the 3 images associated with the item.

Currently i just use a single repeater to SELECT everything. My current SELECT Command for the repeater is:

Select Command:"SELECT * FROM [Maskiner] INNER JOIN images ON images.FK_maskine_id = maskiner.maskine_id INNER JOIN Maskine_kategori ON Maskiner.Maskine_Kategorinavn = Maskine_kategori.Maskine_kategori_id WHERE ([Maskine_kategori_id] = @Maskine_kategori_id)"

i think i have to nest a repeater around where i Eval my image however im not sure what its supposed to SELECT.

i have a Item Detail page aswell where i do want all 3 images associated with the item shown, which i did get to work with a nested repeater that selects images with the sameFK_maskine_idas the current querystring id because the page is called itemdetail.aspx?id=9 etc. So no problem there.

However on the itemCategory.aspx (this page) i just want to display all items with the same Category_id as is in the Current Querystring which works fine at the moment, the problem is i currently repeats out the same item 3 times because each item has 3 images associated with it.

<asp:Repeater ID="Repeater3" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>

        <img class="group list-group-image" src='GalleriImage/<%# Eval("Image_Url") %>' />

        <div class="caption">
            <h4><%#Eval("Maskine_Navn") %></span></h4>

            <p>
                <%# CutText(DataBinder.Eval(Container.DataItem,"Maskine_Beskrivelse"), 123)%>...
            </p>

                <p class="lead text-center">
                    <%#Eval("Maskine_pris") %> kr.
                </p>

                <a href='BrugtProdukt.aspx?id=<%#Eval("Maskine_id") %>
                    <p class="MereInfKnap text-center ">More info</p>
                </a>
    </ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:DatabaseConnectionString1 %>'
    SelectCommand="SELECT * FROM [Maskiner] INNER JOIN Images ON Images.FK_maskine_id = maskiner.maskine_id INNER JOIN Maskine_kategori ON Maskiner.Maskine_Kategorinavn = Maskine_kategori.Maskine_kategori_id WHERE ([Maskine_kategori_id] = @Maskine_kategori_id)">
    <SelectParameters>
        <asp:QueryStringParameter Name="Maskine_kategori_id" QueryStringField="ID"
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

Solution

  • Ok so so i figured it out today, i nested a repeater around my image Eval, and had it select TOP 1 * from images with the same fk_image_id as the record the other repeater (item_id) selected. so my Selectcommand for the nested repeater that handles the images is:

    SelectCommand='<%# "SELECT TOP 1 * FROM [billeder] INNER JOIN maskiner ON maskiner.maskine_id = billeder.FK_maskine_id  WHERE FK_maskine_id =" + Eval("maskine_id") %>'