Damien Sullivan

Damien Sullivan

  • 1.9k
  • 26
  • 3.7k

Repeater Not Correctly Displaying Joined Table Data

Feb 11 2017 12:24 PM
Here is my requirement:
 
I am creating a 'Local Groups' page, which will display different groups. Each group will have a number of photo albums, and each photo album will contain various images.
 
I am currently able to display all the images associated with each group, but the Photo Album name / heading above each image is not the correct one (i.e. that image is not connected to that photo album).
 
The below code displays the following:
These images which are displayed, are linked to the correct groups, 'Red Cross' and 'Gun Club', but those are not the correct Photo Album / Collection names displayed above them.
 
The correct photo album/collection names should be as follows:
 
For first pink flower - "red cross first album"
Black photo - "Red cross second Test"
Green photo - "Blarney event" 
Final pink flower - "second album" 
 
I feel the issue is  in repGroupPhotoGallery_ItemDataBound, but I'm not sure how to fix it. Can anyone please help me? I would really appreciate it. Thanks a lot!
 
Here is my current code: 
 
  1. private void BindRepeater()  
  2. {  
  3.     string constr = ConfigurationManager.ConnectionStrings["FYPConnectionString1"].ConnectionString;  
  4.     using (SqlConnection con = new SqlConnection(constr))  
  5.     {  
  6.         using (SqlCommand cmd = new SqlCommand())  
  7.         {  
  8.             //ORDER BY DATE  
  9.             cmd.CommandText = "select * from Groups WHERE Group_Type ='Group'";  
  10.             cmd.Connection = con;  
  11.             con.Open();  
  12.             repGroupPhotoGallery.DataSource = cmd.ExecuteReader();  
  13.             repGroupPhotoGallery.DataBind();  
  14.             con.Close();  
  15.         }  
  16.     }  
  17. }  
  18. protected void repGroupPhotoGallery_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  19. {  
  20.     DataList datalist = e.Item.FindControl("dlImages"as DataList;  
  21.   
  22.     //find the correct group id of the item  
  23.     string student_Id = DataBinder.Eval(e.Item.DataItem, "Group_Id").ToString();  
  24.     SqlDataAdapter sda = new SqlDataAdapter("select pc_name = pc.Name, s_Id = si.Group_Id, si_filename = si.filename, si_description = si.imageDesc from Groups as s inner join dbo.group_images as si on s.Group_Id = si.group_id inner join dbo.photo_collection_images pci on pci.group_image_id = si.Group_Id inner join dbo.photo_collection as pc on pc.id = pci.photo_collection_id where s.Group_Id =" + student_Id, con);  
  25.     DataTable dt = new DataTable();  
  26.     sda.Fill(dt);  
  27.   
  28.     //bind data to the nested datalist with the Group_Id in the where clause of the query  
  29.     datalist.DataSource = dt;  
  30.     datalist.DataBind();  
  31. }  
  1. <asp:Repeater ID="repGroupPhotoGallery" runat="server" OnItemDataBound="repGroupPhotoGallery_ItemDataBound">  
  2.                            <ItemTemplate>  
  3.                                <div class="col-md-3">  
  4.                                    <div class="panel panel-default">  
  5.                                        <div class="panel-heading">  
  6.                                            <h3 class="panel-title">  
  7.                                                <%# Eval("Group_Name") %>  
  8.                                            h3>  
  9.                                        div>  
  10.                                          
  11.                                        <div class="panel-body">  
  12.                                            <div class="col-md-4 text-center">  
  13.                                                <div class="thumbnail">  
  14.                                                    <asp:DataList ID="dlImages" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" CellPadding="5">  
  15.                                                        <ItemTemplate>  
  16.                                                            <div class="caption">  
  17.                                                                <h3><%# Eval("pc_name") %><br />h3>                                                     
  18.                                                                  <a id="imageLink" href='<%# Eval("si_filename","/Group_Images/{0}") %>' title='<%#Eval("si_description") %>' rel="lightbox[Brussels]">  
  19.                                                                    <asp:Image ID="Image1" ImageUrl='<%# Bind("si_filename", "~/Group_Images/{0}") %>' runat="server" Width="112" Height="84" />  
  20.                                                                a>  
  21.                                                            div>  
  22.                                                        ItemTemplate>  
  23.                                                    asp:DataList>  
  24.                                                div>  
  25.                                            div>  
  26.                                        div>  
  27.                                    div>  
  28.                                      
  29.                                div>  
  30.                                  
  31.                            ItemTemplate>  
  32.                        asp:Repeater>  
Also, here are my SQL tables:
  1. CREATE TABLE [dbo].[Groups] (  
  2.     [Group_Id]   INT            IDENTITY (1, 1) NOT NULL,  
  3.     [Group_Name] NVARCHAR (50)  NULL,  
  4.     [Group_Desc] NVARCHAR (MAX) NULL,  
  5.     [Group_Type] NVARCHAR (50)  NULL,  
  6.     CONSTRAINT [Groups.Group_Id.PrimaryKey] PRIMARY KEY CLUSTERED ([Group_Id] ASC)  
  7. );  
  8.   
  9. CREATE TABLE [dbo].[Group_Images] (  
  10.     [ID]        INT            IDENTITY (1, 1) NOT NULL,  
  11.     [Group_Id]  INT            NOT NULL,  
  12.     [filename]  VARCHAR (250)  NULL,  
  13.     [imageDesc] NVARCHAR (250) NULL,  
  14.     CONSTRAINT [Group_Images.ID.Primary Key] PRIMARY KEY CLUSTERED ([ID] ASC),  
  15.     CONSTRAINT [Group_Images.to.Groups] FOREIGN KEY ([Group_Id]) REFERENCES [dbo].[Groups] ([Group_Id])  
  16. );  
  17.   
  18. CREATE TABLE [dbo].[Photo_Collection] (  
  19.     [Id]       INT            IDENTITY (1, 1) NOT NULL,  
  20.     [Group_Id] INT            NOT NULL,  
  21.     [Name]     NVARCHAR (250) NULL,  
  22.     CONSTRAINT [Photo_Collection.Id.PrimaryKey] PRIMARY KEY CLUSTERED ([Id] ASC),  
  23.     CONSTRAINT [Photo_Collection.to.Groups] FOREIGN KEY ([Group_Id]) REFERENCES [dbo].[Groups] ([Group_Id])  
  24. );  
  25.   
  26. CREATE TABLE [dbo].[Photo_Collection_Images] (  
  27.     [Photo_Collection_Id] INT NOT NULL,  
  28.     [Group_Image_Id]      INT NOT NULL,  
  29.     CONSTRAINT [Photo_Collection_Images.to.Photo_Collection] FOREIGN KEY ([Photo_Collection_Id]) REFERENCES [dbo].[Photo_Collection] ([Id]),  
  30.     CONSTRAINT [Photo_Collection_Images.to.Group_Images] FOREIGN KEY ([Group_Image_Id]) REFERENCES [dbo].[Group_Images] ([ID])  
  31. );  
  
 
 
 
 
 
 
 
 

Brought to you by:

Answers (1)