Пример #1
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());
            }
        }
Пример #2
0
        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();
        }
Пример #3
0
        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());
                }
        }
Пример #4
0
 //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());
     }
 }
Пример #5
0
        //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());
            }
        }