Пример #1
0
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to delete all of the songs shown?", "Delete Songs?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    int count = 0;
                    if (bindingTable.Rows.Count == 0)
                    {
                        MessageBox.Show("No Songs to Delete.");
                        return;
                    }
                    foreach (System.Data.DataRow pianoSong in bindingTable.Rows)
                    {
                        MyPianoSong song = new MyPianoSong();
                        song.songID = Convert.ToInt32(pianoSong["PianoSongID", System.Data.DataRowVersion.Current].ToString().Trim());
                        PianoSongDatabase.deleteSongEntry(song);
                        count++;
                    }
                    bindingTable.AcceptChanges();
                    searchSongs();
                    if (count == 0)
                    {
                        MessageBox.Show("No songs were deleted.");
                    }
                    else
                    {
                        MessageBox.Show(String.Format("Successfully deleted {0} songs!", count));
                    }
                }
            }
            catch (Exception ex)
            {
#if (DEBUG)
                Logger.LogError(ex, showDialog: true);
#else
                Logger.LogError(ex);
#endif
            }
        }
Пример #2
0
        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int count = 0;
                foreach (System.Data.DataRow pianoSong in bindingTable.Rows)
                {
                    if (pianoSong.RowState == System.Data.DataRowState.Added)
                    {
                        if (pianoSong["Title"].ToString().Trim() == "" && pianoSong["Artist"].ToString().Trim() == "" &&
                            pianoSong["Book"].ToString().Trim() == "" && pianoSong["PageNumber"].ToString().Trim() == "")
                        {
                            continue;
                        }
                        MyPianoSong song = new MyPianoSong();
                        count++;
                        try
                        {
                            song.SongTitle  = pianoSong["Title"].ToString().Trim();
                            song.SongArtist = pianoSong["Artist"].ToString().Trim();
                            song.SongBook   = pianoSong["Book"].ToString().Trim();
                            if (pianoSong["PageNumber"].ToString().Trim() == "")
                            {
                                song.PageNum = -1;
                            }
                            else
                            {
                                song.PageNum = Convert.ToInt32(pianoSong["PageNumber"].ToString().Trim());
                            }
                        }
                        catch (Exception exc)
                        {
                            Logger.LogMessage("Song title, artist, or book is not valid, unable to save.", level: Logger.logLevel.Error, showDialog: true);
                            return;
                        }
                        string valid = song.songValidMessage();
                        if (valid.Trim() != "")
                        {
                            throw new Exception(valid);
                        }
                        PianoSongDatabase.addSongEntry(song);
                    }
                    else if (pianoSong.RowState == System.Data.DataRowState.Deleted)
                    {
                        count++;
                        MyPianoSong song = new MyPianoSong();
                        song.songID = Convert.ToInt32(pianoSong["PianoSongID", System.Data.DataRowVersion.Original].ToString().Trim());
                        PianoSongDatabase.deleteSongEntry(song);
                    }
                    else if (pianoSong.RowState == System.Data.DataRowState.Modified)
                    {
                        count++;
                        MyPianoSong song = new MyPianoSong();
                        try
                        {
                            song.songID     = Convert.ToInt32(pianoSong["PianoSongID"].ToString().Trim());
                            song.SongTitle  = pianoSong["Title"].ToString().Trim();
                            song.SongArtist = pianoSong["Artist"].ToString().Trim();
                            song.SongBook   = pianoSong["Book"].ToString().Trim();
                            if (pianoSong["PageNumber"].ToString().Trim() == "")
                            {
                                song.PageNum = -1;
                            }
                            else
                            {
                                song.PageNum = Convert.ToInt32(pianoSong["PageNumber"].ToString().Trim());
                            }
                        }
                        catch (Exception exc)
                        {
                            Logger.LogMessage("Song title, artist, or book is not valid, unable to save.", level: Logger.logLevel.Error, showDialog: true);
                            return;
                        }
                        string valid = song.songValidMessage();
                        if (valid.Trim() != "")
                        {
                            throw new Exception(valid);
                        }
                        PianoSongDatabase.updateSongEntry(song);
                    }
                }
                bindingTable.AcceptChanges();
                searchSongs();
                if (count == 0)
                {
                    MessageBox.Show("No changes were made");
                }
                else
                {
                    MessageBox.Show(String.Format("Successfully saved {0} changes!", count));
                }
            }
            catch (Exception ex)
            {
#if (DEBUG)
                Logger.LogError(ex, showDialog: true);
#else
                Logger.LogError(ex);
#endif
            }
        }