private void buttonSaveArtist_Click(object sender, EventArgs e) { using (var db = new PlayListContext()) { if (db.Database.CanConnect()) { var artists = db.Artists.ToList().OrderBy(artist => artist.ArtistId); int newId = artists.LastOrDefault().ArtistId + 1; bool artistExists = db.Artists.Any(a => a.Name == tbNewArtist.Text); if (tbNewArtist.Text != null || tbNewArtist.Text != "") { if (artistExists) { MessageBox.Show("Artist already exists in Database!"); } else { var result = MessageBox.Show($"Do you want to save the artist {tbNewArtist.Text} to the Database?", "Save artist", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { Artist newArtist = new Artist() { Name = tbNewArtist.Text, ArtistId = newId }; db.Artists.Add(newArtist); db.SaveChanges(); MessageBox.Show("Artist successfully saved!"); } } } else { MessageBox.Show("Artist name can't be blank!"); } } } }
private void buttonSavePL_Click(object sender, EventArgs e) { using (var db = new PlayListContext()) { if (db.Database.CanConnect()) { var playlists = db.Playlists.ToList().OrderBy(pl => pl.PlaylistId); int newId = playlists.LastOrDefault().PlaylistId + 1; if (!string.IsNullOrEmpty(tbPLName.Text)) { Playlist playlist = new Playlist() { Name = tbPLName.Text, PlaylistId = newId }; db.Playlists.Add(playlist); DialogResult result = MessageBox.Show($"Do you want to save the playlist as '{playlist.Name}'?" , "Save playlist", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { db.SaveChanges(); } tbPLName.Clear(); } else { MessageBox.Show("Please enter a title for your playlist!"); } } else { MessageBox.Show("Connection Failed!"); } } }