I have a Group table and an Images table in an SQL Server database. Each image is linked to a different Group when they are inserted into the table.

I am trying to display a separate photo gallery for each row in the Group table.

The below Repeater creates a DataList based on the number of rows in the Groups table. However, it displays all images rather than those which are linked to that specific group.

 <asp:Repeater ID="rptGroupGallery" runat="server" EnableViewState="false">
                                            <ItemTemplate>
                                                <asp:DataList DataKeyField="ID" RepeatDirection="Horizontal" RepeatColumns="3" CellPadding="5" runat="server" DataSourceID="dataSource">
                                                    <ItemTemplate>
                                                        <div style="padding-bottom: 10px;">
                                                            <a id="imageLink" href='<%# Eval("filename","~/SlideImages/{0}") %>' title='<%#Eval("imageDesc") %>' rel="lightbox[Brussels]" runat="server">
                                                                <asp:Image ID="Image1" ImageUrl='<%# Bind("filename", "~/SlideImages/{0}") %>' runat="server" Width="112" Height="84" />
                                                            </a>
                                                        </div>
                                                    </ItemTemplate>
                                                </asp:DataList>
                                            </ItemTemplate>
                                        </asp:Repeater>
                                        <asp:SqlDataSource ID="dataSource" runat="server" ConnectionString="<%$ ConnectionStrings:BallinoraDBConnectionString1 %>" SelectCommand="SELECT * FROM [SlideShowTable]"></asp:SqlDataSource>

C# code below:

 SqlConnection connR;
    string connectionStringR = ConfigurationManager.ConnectionStrings[
        "BallinoraDBConnectionString1"].ConnectionString;
    connR = new SqlConnection(connectionStringR);
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Groups", connR);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    rptGroupGallery.DataSource = dt;
    rptGroupGallery.DataBind();

Below is a sketch of what I would like: sketch

Group SQL:

CREATE TABLE [dbo].[Groups] (
[Group_Id]   INT             IDENTITY (1, 1) NOT NULL,
[Group_Name] NVARCHAR (50)   NULL,
[Group_Desc] NVARCHAR (1000) NULL,
PRIMARY KEY CLUSTERED ([Group_Id] ASC)

);

Slideshow SQL:

CREATE TABLE [dbo].[SlideShowTable] (
[ID]        INT           IDENTITY (1, 1) NOT NULL,
[filename]  VARCHAR (250) NULL,
[imageDesc] VARCHAR (250) NULL,
[groupNo]   INT           NULL);

Also, sample data: smpleData

smpleData

Here are the table structures I am working with:

sql1

sql2

Below is the output when I have 5 rows in the Group table, and 3 images in the Images table, all with different Group_ID's.

screenShot I understand the SqlDataSource's Select Command is "SELECT * FROM tableName".

To seperate the groups, I know that I will need to add a parameter to this command (to pass the various ID's from the Group table).

But I'm not sure how to add that within the repeater so that it counts the number of rows.

Can anyone tell me how to go about this?

Related posts

Recent Viewed