Damien Sullivan

Damien Sullivan

  • NA
  • 26
  • 8.9k

Joined tables not displaying as required

Feb 12 2017 11:25 AM
Hi,
 
Using the below tables, I am trying to retrieve the following:
  • All Group_Name's from the Groups table
  • All Name's from the Photo_Collection table which are linked to a Group (Group_Id)
  • All filename's from Group_Images which are linked to a Photo_Collection (Group_Image_Id).
I want to basically display Photo Albums for each Group using the above.
 
I have tried using the following code:
  1. protected void bindStuff()  
  2. {  
  3.     SqlConnection connR;  
  4.     string connectionStringR = ConfigurationManager.ConnectionStrings[  
  5.         "FYPConnectionString1"].ConnectionString;  
  6.     connR = new SqlConnection(connectionStringR);  
  7.     SqlDataAdapter sda = new SqlDataAdapter(@"DECLARE @Group_Id INT; DECLARE @Photo_Collection_Id INT; DECLARE @Group_Image_Id INT; SELECT @Group_IdGroup_Id = Group_Id, Group_Name FROM Groups SELECT @Photo_Collection_Id = Id, Name FROM Photo_Collection WHERE Group_Id = @Group_Id SELECT @Group_Image_IdGroup_Image_Id = Group_Image_Id FROM Photo_Collection_Images WHERE Photo_Collection_Id = @Photo_Collection_Id SELECT Id, filename, imageDesc FROM Group_Images WHERE ID = @Group_Image_Id;", connR);  
  8.     DataTable dt = new DataTable();  
  9.     sda.Fill(dt);  
  10.     repStuff.DataSource = dt;  
  11.     repStuff.DataBind();  
  12. }  
 But I get this error: "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations."
 
Can someone please help, and tell me what I need to change in this code?
 
Here is my SQL Server table code:
  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.     CONSTRAINT [Groups.Group_Id.PrimaryKey] PRIMARY KEY CLUSTERED ([Group_Id] ASC)  
  6. );  
  7.   
  8. CREATE TABLE [dbo].[Group_Images] (  
  9.     [ID]        INT            IDENTITY (1, 1) NOT NULL,  
  10.     [Group_Id]  INT            NOT NULL,  
  11.     [filename]  VARCHAR (250)  NULL,  
  12.     [imageDesc] NVARCHAR (250) NULL,  
  13.     CONSTRAINT [Group_Images.ID.Primary Key] PRIMARY KEY CLUSTERED ([ID] ASC),  
  14.     CONSTRAINT [Group_Images.to.Groups] FOREIGN KEY ([Group_Id]) REFERENCES [dbo].[Groups] ([Group_Id])  
  15. );  
  16.   
  17. CREATE TABLE [dbo].[Photo_Collection] (  
  18.     [Id]       INT            IDENTITY (1, 1) NOT NULL,  
  19.     [Group_Id] INT            NOT NULL,  
  20.     [Name]     NVARCHAR (250) NULL,  
  21.     CONSTRAINT [Photo_Collection.Id.PrimaryKey] PRIMARY KEY CLUSTERED ([Id] ASC),  
  22.     CONSTRAINT [Photo_Collection.to.Groups] FOREIGN KEY ([Group_Id]) REFERENCES [dbo].[Groups] ([Group_Id])  
  23. );  
  24.   
  25. CREATE TABLE [dbo].[Photo_Collection_Images] (  
  26.     [Photo_Collection_Id] INT NOT NULL,  
  27.     [Group_Image_Id]      INT NOT NULL,  
  28.     CONSTRAINT [Photo_Collection_Images.to.Photo_Collection] FOREIGN KEY ([Photo_Collection_Id]) REFERENCES [dbo].[Photo_Collection] ([Id]),  
  29.     CONSTRAINT [Photo_Collection_Images.to.Group_Images] FOREIGN KEY ([Group_Image_Id]) REFERENCES [dbo].[Group_Images] ([ID])  
  30. );  

Answers (1)