private async void DeleteBtn_Click(object sender, RoutedEventArgs e) { var selected = dataGrid.SelectedItem.ToString(); var post = Post.parseString(selected); var res = MessageBox.Show($"Are you sure you Want to delete Post?", "Database Action", MessageBoxButton.YesNo); if (res == MessageBoxResult.Yes) { using (var context = new myStudyJournalEntities()) { var postToDelete = context.Posts.Find(post.Id); context.Posts.Remove(postToDelete); await context.SaveChangesAsync(); } MessageBox.Show($"Post ID: {post.Id} Deleted Successfully!", "Database Action"); this.viewFrame.Navigate(new DatabasePage(this.viewFrame)); } else { return; } }
private async void onSubmitBtnClicked(object sender, RoutedEventArgs e) { Console.WriteLine(body.Text.Length); if (title.Text != "" && title.Text != "Enter a Title" && listBox2.Items.Count != 0 && body.Text != "") { using (var context = new myStudyJournalEntities()) { if (isEditing) { Post edit = new Post() { Id = this.postToEdit.Id, Date = DateTime.Parse(dateTime.Text), Title = title.Text, Tags = tagsToString(), Body = body.Text }; var confirmEdit = MessageBox.Show("Save edit?", "Database Action", MessageBoxButton.OKCancel); if (confirmEdit == MessageBoxResult.OK) { var selected = context.Posts.Find(this.postToEdit.Id); context.Entry(selected).CurrentValues.SetValues(edit); await context.SaveChangesAsync(); MessageBox.Show("edit saved", "Database Action"); } else { return; } } else { Post entry = new Post() { Date = DateTime.Parse(dateTime.Text), Title = title.Text, Tags = tagsToString(), Body = body.Text }; var confirmation = MessageBox.Show("Add post?", "Database Action", MessageBoxButton.OKCancel); if (confirmation == MessageBoxResult.OK) { context.Posts.Add(entry); await context.SaveChangesAsync(); MessageBox.Show("Post saved", "Database Action"); } else { return; } } } } else { MessageBox.Show("All fields must be entered to post.", "Error"); return; } string tagsToString() { string[] tags = new string[listBox2.Items.Count]; listBox2.Items.CopyTo(tags, 0); string items = String.Join(",", tags).ToUpper(); return(items); } }