Data/BookRepository.cs: ` using BookViewer.Model; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BookViewer.Data { public class BookRepository { private SqlConnection _connection; public BookRepository() { _connection = new SqlConnection(Properties.Settings.Default.connectionstring); } public List GetBooks() { List books = new List(); _connection.Open(); using (SqlCommand command = new SqlCommand("SELECT * FROM Books;", _connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { books.Add(new Book( (string)reader["Id"], (string)reader["Author"], (string)reader["Title"], (string)reader["Genre"], (double)reader["Price"], (DateTime)reader["PublishDate"], (string)reader["Description"] )); } } } _connection.Close(); return books; } public void UpdateBook(Book book) { _connection.Open(); string query = @"UPDATE Books SET Author = @Author, Title = @Title WHERE Id = @Id;"; using (SqlCommand command = new SqlCommand(query, _connection)) { command.Parameters.AddWithValue("@Author", book.Author); command.Parameters.AddWithValue("@Title", book.Title); command.Parameters.AddWithValue("@Id", book.Id); command.ExecuteNonQuery(); } _connection.Close(); } public void InsertBook(Book book) { _connection.Open(); string query = "INSERT INTO Books (Id, Author, Title, Genre, Price, PublishDate, Description) VALUES (@Id, @Author, @Title, @Genre, @Price, @PublishDate, @Description);"; using (SqlCommand command = new SqlCommand(query, _connection)) { command.Parameters.AddWithValue("@Id", book.Id); command.Parameters.AddWithValue("@Author", book.Author); command.Parameters.AddWithValue("@Title", book.Title); command.Parameters.AddWithValue("@Genre", book.Genre); command.Parameters.AddWithValue("@Price", book.Price); command.Parameters.AddWithValue("@PublishDate", book.PublishDate); command.Parameters.AddWithValue("@Description", book.Description); command.ExecuteNonQuery(); } _connection.Close(); } } } ` -------------------------------------------------------------------------------------------------------------- Model/Book.cs: ` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BookViewer.Model { public class Book { public string Id { get; set; } public string Author { get; set; } public string Title { get; set; } public string Genre { get; set; } public double Price { get; set; } public DateTime PublishDate { get; set; } public string Description { get; set; } public Book(string id, string author, string title, string genre, double price, DateTime publishDate, string description) { Id = id; Author = author; Title = title; Genre = genre; Price = price; PublishDate = publishDate; Description = description; } public override string ToString() { return this.Title; } } } ` ----------------------------------------------------------------------------------------------------------------- ViewModel/BookCatalougePageViewModel.cs: ` using BookViewer.Data; using BookViewer.Model; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BookViewer.ViewModel { public class BookCatalougePageViewModel { private BookRepository _repository; public ObservableCollection Books { get; set; } public Book SelectedBook { get; set; } public string NewTitle { get; set; } public string NewAuthor { get; set; } public string NewDescription { get; set; } public DateTime NewPublishDate { get; set; } public string NewId { get; set; } public double? NewPrice { get; set; } public string NewGenre { get; set; } public BookCatalougePageViewModel(BookRepository repository) { _repository = repository; Books = new ObservableCollection(_repository.GetBooks()); } public void UpdateBook(Book book) { if (book == null) return; _repository.UpdateBook(book); } public void InsertBook(Book book) { _repository.InsertBook(book); Books.Add(book); } } } ` --------------------------------------------------------------------------------------------------------- ViewModel/BookDetailsPageViewModel.cs ` using BookViewer.Data; using BookViewer.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BookViewer.ViewModel { public class BookDetailsPageViewModel { private BookRepository _repository; public Book Book { get; } public BookDetailsPageViewModel(BookRepository repository, Book book) { _repository = repository; Book = book; } } } ` ----------------------------------------------------------------------------------------------------------------- xaml and xaml.cs files: App.xaml and App.xaml.cs: ` ----------------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows; namespace BookViewer { /// /// Interaktionslogik für "App.xaml" /// public partial class App : Application { } } ` --------------------------------------------------------------------------------------------- BookCatalougePage.xaml and .xaml.cs: ` ------------------------------------------------------------------------------------------------------------------------------------------------------- using BookViewer.Data; using BookViewer.ViewModel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using BookViewer.Model; namespace BookViewer { /// /// Interaktionslogik für BookCatalougePage.xaml /// public partial class BookCatalougePage : Page { private BookRepository _repository; private BookCatalougePageViewModel _viewModel; public BookCatalougePage() { _repository = new BookRepository(); _viewModel = new BookCatalougePageViewModel(_repository); InitializeComponent(); this.DataContext = _viewModel; } private void View_Button_Click(object sender, RoutedEventArgs e) { if (_viewModel.SelectedBook == null) return; NavigationService.Navigate(new BookDetailsPage(_repository, _viewModel.SelectedBook)); } private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { e.EditingElement.GetBindingExpression(TextBox.TextProperty)?.UpdateSource(); this._viewModel.UpdateBook((Book)e.Row.Item); } private void Add_Book_Button_Click(object sender, RoutedEventArgs e) { if (_viewModel.NewId == null || _viewModel.NewAuthor == null || _viewModel.NewTitle == null || _viewModel.NewGenre == null || _viewModel.NewPrice == null || _viewModel.NewPublishDate == null || _viewModel.NewDescription == null || _viewModel.NewPublishDate.Year < 1753) { MessageBox.Show("Please Enter valid values in each field"); return; } Book newBook = new Book( _viewModel.NewId, _viewModel.NewAuthor, _viewModel.NewTitle, _viewModel.NewGenre, (double) _viewModel.NewPrice, _viewModel.NewPublishDate, _viewModel.NewDescription ); _viewModel.InsertBook(newBook); } } } ` ------------------------------------------------------------------------------------------------------------ BookDetailsPage.xaml and .xaml.cs: ` ----------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using BookViewer.Data; using BookViewer.Model; using BookViewer.ViewModel; namespace BookViewer { /// /// Interaktionslogik für BookDetailsPage.xaml /// public partial class BookDetailsPage : Page { private BookDetailsPageViewModel _viewModel; public BookDetailsPage(BookRepository _repository, Book selectedBook) { this._viewModel = new BookDetailsPageViewModel(_repository, selectedBook); InitializeComponent(); this.DataContext = _viewModel; } } } `