private void btnAddnew_Click(object sender, RoutedEventArgs e) { //Create a photo object to pass in to the addnew form - the arguments will be supplied within that form Photograph newphoto = new Photograph(null, DateTime.MinValue, DateTime.MinValue, null, null, null, null); //Create an editing form, passing in a new photo and a bool that tells the form whether we're editing or adding a record. addnew frm = new addnew(newphoto, false); if (frm.ShowDialog() == true) { //Add newphoto to plist plist.Add(newphoto); DatabaseUtil.AddRecord(newphoto); CollectionViewSource.GetDefaultView(dgPhotoInfo.ItemsSource).Refresh(); } }
private void btnDelete_Click(object sender, RoutedEventArgs e) { CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(dgPhotoInfo.ItemsSource); //This accesses the currently selected photo object Photograph currentphoto = view.CurrentItem as Photograph; //then creates the form, passing in the currentphoto object for editing, along with the boolean true for 'isedit' //addnew frm = new addnew(currentphoto, true); MessageBoxResult confirmdelete = MessageBox.Show("Are you sure you want to delete this record?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question); if (confirmdelete == MessageBoxResult.Yes) { CollectionViewSource.GetDefaultView(dgPhotoInfo.ItemsSource).Refresh(); DatabaseUtil.DeleteRecord(currentphoto, plist); } }
private void btnEdit_Click(object sender, RoutedEventArgs e) { //Reuses the 'addnew' form to edit an existing record CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(dgPhotoInfo.ItemsSource); //This accesses the currently selected photo object Photograph currentphoto = view.CurrentItem as Photograph; //then creates the form, passing in the currentphoto object for editing, along with the boolean true for 'isedit' addnew frm = new addnew(currentphoto, true); if (frm.ShowDialog() == true) { //Refresh the view if we've changed a record CollectionViewSource.GetDefaultView(dgPhotoInfo.ItemsSource).Refresh(); DatabaseUtil.EditRecord(currentphoto); } }
/// <summary> /// If the database is empty, this method just creates some initial data to populate the database /// </summary> public static void PopulateEmptyDatabase() { //Add some initial records to the database Photograph photo = new Photograph("Cadence and Gawen Studio", Convert.ToDateTime("6/14/2005"), Convert.ToDateTime("2/28/2017"), "Studio photo of both kids together.", "Picture People", "studio, basket, cadence, gawen", @"C:\Users\Mary Sturgeon\Pictures\CadenceandGawen3.jpg"); Photograph photo2 = new Photograph("Cadence and Gawen Easter", Convert.ToDateTime("4/30/2008"), Convert.ToDateTime("2/18/2017"), "Studio photo of both kids at Easter", "Picture People", "spring, easter, bunny, cadence, gawen", @"C:\Users\Mary Sturgeon\Pictures\Picture2.jpg"); Photograph photo3 = new Photograph("Orange Crush", Convert.ToDateTime("3/08/2014"), Convert.ToDateTime("2/15/2017"), "Anaia Latrix", "Mary Sturgeon", "roller derby, cadence, skating", @"C:\Users\Mary Sturgeon\Pictures\20140308_175124.jpg"); Photograph photo4 = new Photograph("Before Camp", Convert.ToDateTime("8/1/2015"), Convert.ToDateTime("2/16/2017"), "Kids together before summer camp", "Jason Sturgeon", "orkila, camp, cadence, gawen", @"C:\Users\Mary Sturgeon\Pictures\20150801_085752.jpg"); Photograph photo5 = new Photograph("Fall Out Boy Concert", Convert.ToDateTime("8/2/2015"), Convert.ToDateTime("2/1/2017"), "Mom and Gawen at Fall Out Boy Concert", "Jason Sturgeon", "concert, mom, gawen", @"C:\Users\Mary Sturgeon\Pictures\20150802_185025.jpg"); Photograph photo6 = new Photograph("Hawaii Zipline", Convert.ToDateTime("11/10/2017"), Convert.ToDateTime("2/4/2017"), "Kids on zipline in Hawaii", "Kauai Adventures", "hawaii, zipline, cadence, gawen", @"C:\Users\Mary Sturgeon\Pictures\ZiplineHawaii.jpg"); AddRecord(photo); AddRecord(photo2); AddRecord(photo3); AddRecord(photo4); AddRecord(photo5); AddRecord(photo6); }
public static void DeleteRecord(Photograph photo, PhotoList list) { list.Remove(photo); using (SqlConnection connection = new SqlConnection(connectionString)) { try { using (SqlCommand command = new SqlCommand($"DELETE FROM tblPhotoAlbum WHERE Id = @Id", connection)) { command.Parameters.AddWithValue("@Id", photo.Id); connection.Open(); command.ExecuteNonQuery(); } } catch (SqlException e) { MessageBox.Show("An exception occurred:" + e.Message); } } }
/// <summary> /// This method Reads all records from the database into the list /// </summary> /// <param name="list">This is the list that should be populated with records from the database</param> public static void ReadDatabase(PhotoList list) { //Read all database records into photolist using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("SELECT * FROM tblPhotoAlbum", connection); try { connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Photograph photo = new Photograph((string)reader["Title"], Convert.ToDateTime(reader["DateTaken"]), Convert.ToDateTime(reader["DateMod"]), (string)reader["Description"], (string)reader["Photographer"], (string)reader["Keywords"], (string)reader["FileLocation"]); photo.Id = (int)reader["ID"]; list.Add(photo); } } catch (SqlException e) { MessageBox.Show("An exception occurred:" + e.Message); } } }