private async void AddInventoryBtn_Executed(object sender, ExecutedRoutedEventArgs e) { AddInventoryModel context = this.DataContext as AddInventoryModel; if (context.IsNull()) { CustomMessageBox.ShowOK("Not enough information for inventory. Please try again.", "Error", "OK", MessageBoxImage.Error); return; } Inventory inventory = GetInventoryFromContext(); using (SQLiteConnection connection = new SQLiteConnection(SqlHelpers.ConnectionString)) { connection.Open(); using (var cmd = connection.CreateCommand()) { cmd.CommandText = "SELECT Number FROM Inventory WHERE Number = @Number"; cmd.Parameters.AddWithValue("Number", inventory.Number); using (var reader = await cmd.ExecuteReaderAsync()) { if (reader.HasRows) { CustomMessageBox.ShowOK("The Inventory has already existed", "Error", "OK", MessageBoxImage.Error); return; } } cmd.CommandText = "INSERT INTO Inventory ([Number], [Object], [Incoming Date], [Repack], [Price]) VALUES (@Number, @Object, @InDate, @Repack, @Price)"; cmd.Parameters.AddWithValue("Number", inventory.Number); cmd.Parameters.AddWithValue("Object", inventory.Object); cmd.Parameters.AddWithValue("InDate", inventory.InDate); cmd.Parameters.AddWithValue("Repack", inventory.Repack); cmd.Parameters.AddWithValue("Price", inventory.Price); var res = await cmd.ExecuteNonQueryAsync(); if (res != -1) { CustomMessageBox.ShowOK("Inventory added successfully.", "Information", "OK", MessageBoxImage.Information); context.ObjectNumbers.Insert(0, inventory.Number); ClearContext(); return; } CustomMessageBox.ShowOK("Something wrong while adding inventory. Please try again later.", "Error", "OK", MessageBoxImage.Error); } } }
private void AddInventoryBtn_CanExecute(object sender, CanExecuteRoutedEventArgs e) { AddInventoryModel context = this.DataContext as AddInventoryModel; e.CanExecute = context.IsNull() == false; }