I'm working on a school project
I want to get all the information of the bands, which works fine. Until i get to genres. Genre is a bit special, because a band can have multiple genres and vice versa.
So in SQL I have a table Bands, a table Genre and a table Band_Genre If i do the inner join there
SELECT *
FROM Band
INNER JOIN Band_Genre
ON Band.ID = Band_Genre.BandID
Everything works fine in SQL but when i try a second reader with that statement it returns null.
But what i want to do now is fill the datagrid with the genres themselves, not the ID's. So what i think is that i need to get the ID out of the Inner join and link it to Genre.Name
Maybe this goes a bit far but I would be happy if someone could already get the id's into the ObservableCollection. So that the Datagrid shows Genres: 1, 3 for example.
Thanks in advance
C#
//Property
private ObservableCollection genres;
public ObservableCollection Genres
{
get { return genres; }
set { genres = value; }
}
//Method
public static ObservableCollection GetBands()
{
ObservableCollection list = new ObservableCollection();
try
{
string provider = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DbConnection con = DbProviderFactories.GetFactory(provider).CreateConnection();
con.ConnectionString = connectionstring;
con.Open();
DbCommand command = DbProviderFactories.GetFactory(provider).CreateCommand();
command.CommandType = System.Data.CommandType.Text;
command.Connection = con;
command.CommandText = "SELECT * FROM Band";
//Bands ophalen
DbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Band b = new Band()
{
ID = reader["ID"].ToString(),
Name = reader["Name"].ToString(),
Picture = reader["Picture"].ToString(),
Description = reader["Description"].ToString(),
Twitter = reader["Twitter"].ToString(),
Facebook = reader["Facebook"].ToString(),
//Genres = (Genre)reader["Genres"] lijst van genres kunnen doorgeven, probleem met meerdere genres per band. Kijken filmpje inner joint
};
if (!DBNull.Value.Equals(reader["Picture"]))
{
b.Picture = reader["Picture"].ToString();
}
else
{
b.Picture = null;
}
list.Add(b);
}
//Genres ophalen via INNER JOIN
DbCommand command2 = DbProviderFactories.GetFactory(provider).CreateCommand();
command2.CommandType = System.Data.CommandType.Text;
command2.Connection = con;
command2.CommandText = "SELECT * FROM Band INNER JOIN Band_Genre On Band.ID = Band_Genre.BandID";
//Bands ophalen
DbDataReader reader2 = command2.ExecuteReader();
while (reader2.Read())
{
Genre g = new Genre()
{
ID = reader2["GenreID"].ToString(),
//Name = reader2[
};
}
reader.Close();
reader2.Close();
con.Close();
}
XAML: