private void btnAddSong_Click(object sender, EventArgs e) { int alId = Convert.ToInt32(comboBoxAddSongAlbum.SelectedValue); var song = new Song { ArtistId = Convert.ToInt32(comboBoxAddSongArtist.SelectedValue), Title = textBoxAddSongTitle.Text, AlbumId = Convert.ToInt32(comboBoxAddSongAlbum.SelectedValue), TrackNumber = Convert.ToInt32(numericUpDownAddSongTrackNumber.Value), GenreID = Convert.ToInt32(comboBoxAddSongGenre.SelectedValue), TrackLength = Convert.ToInt32(numericUpDownAddSongTrackLength.Value), }; try { using (var context = new CDCatalogContext()) { if (context.Songs.Any(x => x.AlbumId == alId && x.Title == textBoxAddSongTitle.Text)) { MessageBox.Show("Cannot add duplicate songs on the same album "); } else { context.Songs.Add(song); context.SaveChanges(); } } } catch (Exception ex) { MessageBox.Show("There was a problem adding/saving this song:" + ex.Message.ToString()); } }
private void btnAddCD_Click(object sender, EventArgs e) { //Upon clicking the Add Album button, create album object then using linq, save it to the database. int artId = Convert.ToInt32(comboBoxAddCDArtist.SelectedValue); var album = new Album { ArtistId = artId, Title = textBoxAddCDTitle.Text, Year = Convert.ToInt32(numericUpDownAddCDYear.Value), }; try { using (var context = new CDCatalogContext()) { if (context.Albums.Any(x => x.ArtistId == artId && x.Title == textBoxAddCDTitle.Text)) { MessageBox.Show("Cannot add already existing album"); } else { context.Albums.Add(album); context.SaveChanges(); } } } catch (Exception ex) { MessageBox.Show("There was a problem saving this album:" + ex.Message.ToString()); } refreshComboBoxes(); }
private int GetArtistId(string p) { try { using (var context = new CDCatalogContext()) { var artistid = context.Artists .Where(a => a.Name.ToUpper() == p.ToString().ToUpper()); //First check to see if Artist already in database if (artistid.Any()) { return artistid.FirstOrDefault().ID; } else { // Artist is not in the database, Insert new artist and return ID InsertNewArtist(p); var artistid2 = context.Artists .Where(a => a.Name.ToUpper() == p.ToString().ToUpper()); return artistid2.FirstOrDefault().ID; } } } catch (Exception ex) { MessageBox.Show("There was a problem getting the Artist ID:" + ex.Message.ToString()); return 0; } }
private int GetGenreId(string p) { try { using (var context = new CDCatalogContext()) { var genreID = context.Genres .Where(a => a.Name.ToUpper() == p.ToString().ToUpper()); //First check to see if Genre already in database if (genreID.Any()) { return genreID.FirstOrDefault().ID; } else { // Genre is not in the database, Insert new genre and return ID InsertNewGenre(p); var genreID2 = context.Genres .Where(a => a.Name.ToUpper() == p.ToString().ToUpper()); return genreID2.FirstOrDefault().ID; } } } catch (Exception ex) { MessageBox.Show("There was a problem getting Genre Id:" + ex.Message.ToString()); return 0; } }
private static void InsertRating(int i, string t, int r) { try { using (var context = new CDCatalogContext()) { if (t == "Song") { Song s = (from x in context.Songs where x.Id == i select x).FirstOrDefault(); s.Rating = r; context.SaveChanges(); } else { Album a = (from x in context.Albums where x.Id == i select x).FirstOrDefault(); a.Rating = r; context.SaveChanges(); } } } catch (Exception ex) { MessageBox.Show("There was a problem inserting a Rating:" + ex.Message.ToString()); } }
private void generatePlaylist(int maxplaylistLength) { try { using (var context = new CDCatalogContext()) { var SongArtistAlbumList = from ts in context.Songs join n in context.Artists on ts.ArtistId equals n.ID join a in context.Albums on ts.AlbumId equals a.Id where ts.TrackLength <= maxplaylistLength //changes index numbers (shuffles list) orderby Guid.NewGuid() select new { ts, n, a }; var playlistDetails = new List<Details>(); int totalTime = 0; //If a song is less than the max playlist length and if added to the playlist will make //the total playlist time less than or equal to the max playlist length, then add the song to the playlist. foreach (var item in SongArtistAlbumList) { int songTrackLength = item.ts.TrackLength; if (songTrackLength <= maxplaylistLength && (totalTime + songTrackLength) <= maxplaylistLength) { playlistDetails.Add(new Details() { Title = item.ts.Title.ToString(), Artist = item.n.Name, AlbumTitle = item.a.Title, TrackLength = item.ts.TrackLength }); totalTime = totalTime + songTrackLength; } } dataGridViewPlaylist.DataSource = playlistDetails; dataGridViewPlaylist.Columns[0].Visible = false; dataGridViewPlaylist.Columns[4].Visible = false; dataGridViewPlaylist.Columns[5].Visible = false; dataGridViewPlaylist.Columns[7].Visible = false; dataGridViewPlaylist.Columns[8].Visible = false; } } catch (Exception ex) { MessageBox.Show("There was a problem generating playlist:" + ex.Message.ToString()); } }
//This method refreshes the combo boxes so that they're always up-to-date, whenever a new albums, artists, or genres are added. private void refreshComboBoxes() { try { using (var context = new CDCatalogContext()) { var Artistlist = context.Artists.OrderBy(a => a.Name).ToList(); var Genrelist = context.Genres.OrderBy(g => g.Name).ToList(); //var Songlist = context.Songs.OrderBy(s => s.Title).ToList(); var CDlist = context.Albums.OrderBy(c => c.Title).ToList(); var MergedSongCDlist = new List<CombinedSongsAlbums>(); //foreach (var item in Songlist) //{ // MergedSongCDlist.Add(new CombinedSongsAlbums() // { // Title = "Song - " + item.Title, // ID = item.Id, // AssetType = "Song" // }); //} foreach (var item in CDlist) { MergedSongCDlist.Add(new CombinedSongsAlbums() { Title = "CD - " + item.Title, ID = item.Id, AssetType = "Album" }); } this.comboBoxAddSongArtist.DataSource = Artistlist; this.comboBoxAddSongArtist.DisplayMember = "Name"; this.comboBoxAddSongArtist.ValueMember = "ID"; this.comboBoxAddSongAlbum.DataSource = CDlist; this.comboBoxAddSongAlbum.DisplayMember = "Title"; this.comboBoxAddSongAlbum.ValueMember = "Id"; this.comboBoxAddSongGenre.DataSource = Genrelist; this.comboBoxAddSongGenre.DisplayMember = "Name"; this.comboBoxAddSongGenre.ValueMember = "ID"; } } catch (Exception ex) { MessageBox.Show("There was a problem refreshing combo boxes:" + ex.Message.ToString()); } }
//Method to insert Genre. Takes parameter p to represent the name of the Genre to add. private static void InsertNewGenre(string p) { var genre = new Genre { Name = p }; try { using (var context = new CDCatalogContext()) { context.Genres.Add(genre); context.SaveChanges(); } } catch (Exception ex) { MessageBox.Show("There was a problem inserting Genre in the database:" + ex.Message.ToString()); } }
//Method to insert Artist. Takes parameter p to represent the name of the Artist to add. private static void InsertNewArtist(string p) { var artist = new Artist { Name = p }; try { using (var context = new CDCatalogContext()) { context.Artists.Add(artist); context.SaveChanges(); } } catch (Exception ex) { MessageBox.Show("There was a problem inserting an Artist into the database:" + ex.Message.ToString()); } }
private void showDetails(string assettype, int id) { dataGridViewDetails.DataSource = null; labelDetailsInfo.Text = ""; labelDetailsAlbumTitle.Text = ""; labelDetailsArtist.Text = ""; labelDetailsYear.Text = ""; labelDetailsRating.Text = ""; if (assettype == "Song") { labelDetailsInfo.Text = "Below are additional details of the song selected above:"; try { using (var context = new CDCatalogContext()) { var SongArtistGenreList = from ts in context.Songs join n in context.Artists on ts.ArtistId equals n.ID join g in context.Genres on ts.GenreID equals g.ID join a in context.Albums on ts.AlbumId equals a.Id where ts.Id == id select new { ts, n, g, a }; var SongDetails = new List<Details>(); foreach (var item in SongArtistGenreList) { SongDetails.Add(new Details() { AssetType = "Song", Title = item.ts.Title.ToString(), Artist = item.n.Name, AlbumTitle = item.a.Title, TrackNumber = Convert.ToInt32(item.ts.TrackNumber), Genre = item.g.Name, TrackLength = Convert.ToInt32(item.ts.TrackLength), Rating = Convert.ToInt32(item.ts.Rating) }); } dataGridViewDetails.DataSource = SongDetails; dataGridViewDetails.Columns[0].Visible = false; dataGridViewDetails.Columns[7].Visible = false; dataGridViewDetails.ReadOnly = true; //remove highlighting in details dataGridViewDetails.ClearSelection(); dataGridViewDetails.CurrentCell = null; } } catch (Exception ex) { MessageBox.Show("There was a problem showing Song details:" + ex.Message.ToString()); } } else if (assettype == "CD") { try { using (var context = new CDCatalogContext()) { //ta = title album, n = name var AlbumArtistList = from ta in context.Albums join n in context.Artists on ta.ArtistId equals n.ID where ta.Id == id select new { ta, n }; //ts = title song var AlbumSongList = from ts in context.Songs join ta in context.Albums on ts.AlbumId equals ta.Id where ta.Id == id orderby ts.TrackNumber ascending select new { ts, ta }; var SongDetails = new List<Details>(); foreach (var item in AlbumSongList) { SongDetails.Add(new Details() { Title = item.ts.Title.ToString(), TrackNumber = Convert.ToInt32(item.ts.TrackNumber), TrackLength = Convert.ToInt32(item.ts.TrackLength) }); } labelDetailsInfo.Text = "The following are additional details and list of songs on the album selected above:"; foreach (var item in AlbumArtistList) { labelDetailsAlbumTitle.Text = "Album Title: " + item.ta.Title; labelDetailsArtist.Text = "Artist: " + item.n.Name; labelDetailsYear.Text = "Year: " + item.ta.Year.ToString(); labelDetailsRating.Text = "Rating: " + item.ta.Rating.ToString(); } dataGridViewDetails.DataSource = SongDetails; dataGridViewDetails.Columns[0].Visible = false; dataGridViewDetails.Columns[2].Visible = false; dataGridViewDetails.Columns[3].Visible = false; dataGridViewDetails.Columns[5].Visible = false; dataGridViewDetails.Columns[6].Visible = false; dataGridViewDetails.Columns[7].Visible = false; dataGridViewDetails.Columns[8].Visible = false; dataGridViewDetails.ReadOnly = true; //remove highlighting in details dataGridViewDetails.ClearSelection(); dataGridViewDetails.CurrentCell = null; } } catch (Exception ex) { MessageBox.Show("There was a problem showing Album details:" + ex.Message.ToString()); } } }
private void populateDataGridViewFind(string keyword, string searchtype) { using (var context = new CDCatalogContext()) { try { switch (searchtype) { case "Title": var AlbumArtistList = from ta in context.Albums join n in context.Artists on ta.ArtistId equals n.ID where (ta.Title.Contains(keyword)) select new { ta, n }; var SongArtistList = from ts in context.Songs join n in context.Artists on ts.ArtistId equals n.ID where (ts.Title.Contains(keyword)) select new { ts, n }; if (AlbumArtistList.Any() == false && SongArtistList.Any() == false) { MessageBox.Show("There were no Songs or CDs matching your search criteria."); break; } var MergedSongCDlist = new List<CombinedSongsAlbums>(); foreach (var item in AlbumArtistList) { MergedSongCDlist.Add(new CombinedSongsAlbums() { AssetType = "CD", Title = item.ta.Title, Artist = item.n.Name, ID = item.ta.Id }); } foreach (var item in SongArtistList) { MergedSongCDlist.Add(new CombinedSongsAlbums() { AssetType = "Song", Title = item.ts.Title, Artist = item.n.Name, ID = item.ts.Id, }); } dataGridViewFindSongCD.DataSource = MergedSongCDlist; dataGridViewFindSongCD.Columns[3].Visible = false; dataGridViewFindSongCD.ReadOnly = true; break; case "Artist": { var AlbumArtistList2 = from ta in context.Albums join n in context.Artists on ta.ArtistId equals n.ID where (n.Name.Contains(keyword)) select new { ta, n }; var SongArtistList2 = from ts in context.Songs join n in context.Artists on ts.ArtistId equals n.ID where (n.Name.Contains(keyword)) select new { ts, n }; if (AlbumArtistList2.Any() == false && SongArtistList2.Any() == false) { MessageBox.Show("There were no Songs or CDs matching your search criteria."); break; } var MergedSongCDlist2 = new List<CombinedSongsAlbums>(); foreach (var item in AlbumArtistList2) { MergedSongCDlist2.Add(new CombinedSongsAlbums() { AssetType = "CD", Title = item.ta.Title, Artist = item.n.Name, ID = item.ta.Id }); } foreach (var item in SongArtistList2) { MergedSongCDlist2.Add(new CombinedSongsAlbums() { AssetType = "Song", Title = item.ts.Title, Artist = item.n.Name, ID = item.ts.Id, }); } dataGridViewFindSongCD.DataSource = MergedSongCDlist2; dataGridViewFindSongCD.Columns[3].Visible = false; dataGridViewFindSongCD.ReadOnly = true; } break; case "Genre": { var AlbumSongArtistGenreList = from ta in context.Albums join ts in context.Songs on ta.Id equals ts.AlbumId join g in context.Genres on ts.GenreID equals g.ID join n in context.Artists on ts.ArtistId equals n.ID where (g.Name.Contains(keyword)) select new { ta, ts, g, n }; if (AlbumSongArtistGenreList.Any() == false) { MessageBox.Show("There were no Songs or CDs matching your search criteria."); break; } var MergedSongCDlist3 = new List<CombinedSongsAlbums>(); foreach (var item in AlbumSongArtistGenreList) { MergedSongCDlist3.Add(new CombinedSongsAlbums() { AssetType = "Song", Title = item.ts.Title, Artist = item.n.Name, ID = item.ts.Id }); MergedSongCDlist3.Add(new CombinedSongsAlbums() { AssetType = "CD", Title = item.ta.Title, Artist = item.n.Name, ID = item.ta.Id }); } dataGridViewFindSongCD.DataSource = MergedSongCDlist3; dataGridViewFindSongCD.Columns[3].Visible = false; dataGridViewFindSongCD.ReadOnly = true; } break; } } catch (Exception ex) { MessageBox.Show("There was a problem displaying search results:" + ex.Message.ToString()); } } }