Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a Genere Class as below
C#
public class Genre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }
        public string Description {get; set;}
        public List<Album> Albums { get; set; }
    }


I am trying to populate the Genre objet by using below code in my DAL class
public List<Genre> GetAlbumByGenreName(string genre)
{
    IList<Genre> genres = new List<Genre>();
    IList<Album> albums = new List<Album>();
    Database db = DatabaseFactory.CreateDatabase();
    DbConnection cn = db.CreateConnection();
    cn.Open();

    DbCommand cmd = cn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "Select g.GenreId,Name,Description, A.Title from Genre g Inner Join Album A on g.GenreId = a.GenreId Where g.Name = '" + genre + "'";
    using (SqlDataReader dr = cmd.ExecuteReader() as SqlDataReader)
    {
        while (dr.Read())
        {
            genres.Add(new Genre
            {
                GenreId = dr.GetInt32(0),
                Name = dr.GetString(1),
                Description = dr.GetString(2)//,
                //Albums = dr.GetString(3)
            });
        }
    }
    return genres.ToList();
}


My Question is how i can populate the Albums member of Genre Class.
In my database one Genre can have multiple Albums and I have to Use Enterprise Library's Data Access Application Data Block.

Thanks in Advance.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Mar-12 15:07pm    
Not clear what's the problem.
--SA
eajay 19-Mar-12 15:15pm    
Problem is how can i populate the Album Member of the Genre class in teh DAL Class method?
Sergey Alexandrovich Kryukov 19-Mar-12 15:48pm    
You just repeated what you already said. But what makes is a problem.
Well, I don't see where you get data from data reader, something like object data = dr["myColumnName"]; where is that?
--SA
eajay 19-Mar-12 16:02pm    
This is whare i am getting the data
SqlDataReader dr = cmd.ExecuteReader() as SqlDataReader (in the Useing statement in the Methos under my DAL Class)
Sergey Alexandrovich Kryukov 19-Mar-12 17:33pm    
Sorry, I did not realize you do GetString, etc. Still don't see what's the problem?
--SA

1 solution

Since you get multiple rows with the same genre (and different albums), you must populate all the rows and keep track of the already parsed genres. For example find the genre in the genres list. When the genre already exists just add the a new album to this object otherwise add a new genre to the list (and of course the news read album).

Adding a order by genreid can help you to simplify it. Then you only have to remember the previous genreid.

Regards
Piet
 
Share this answer
 
Comments
eajay 19-Mar-12 19:47pm    
Thanks for your reply but, Can you please add a sample code to do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900