/// <summary> /// Returns a list of all Item Definitions /// </summary> /// <returns></returns> public List <ItemDesc> MakeItemDescList() { try { //grab all itemdescs from the database int retVal = 0; DataSet ds = DataAccess.ExecuteSQLStatement(DataSQL.SelectAllItemDesc(), ref retVal); List <ItemDesc> retList = new List <ItemDesc>(); for (int i = 0; i < retVal; i++) { ItemDesc temp = new ItemDesc(); temp.ItemCode = ds.Tables[0].Rows[i].ItemArray[0].ToString(); temp.Description = ds.Tables[0].Rows[i].ItemArray[1].ToString(); if (double.TryParse(ds.Tables[0].Rows[i].ItemArray[2].ToString(), out double cost)) { temp.Cost = cost; } retList.Add(temp); } return(retList); } catch (Exception ex) { //Throw a custom-trace exception throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
/// <summary> /// Event handler for datagrid selection changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgItemDefs_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { //Clears textboxes or loads them with information for the selection if (dgItemDescs.SelectedIndex == -1) { txtCost.Clear(); txtDescription.Clear(); txtItemCode.Clear(); } else { ItemDesc item = dgItemDescs.SelectedItem as ItemDesc; txtDescription.Text = item.Description; txtItemCode.Text = item.ItemCode; txtCost.Text = item.Cost.ToString(); } } catch (Exception ex) { //Throw a custom-trace exception throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
/// <summary> /// Adds a new itemdesc to the database. /// Returns false if the item errors due to duplicate primary key /// </summary> /// <param name="item"></param> public bool AddNewItemDesc(ItemDesc item) { try { DataAccess.ExecuteNonQuery(DataSQL.InsertItemDesc(item.ItemCode, item.Description, item.Cost)); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Deletes the passed itemdesc from the database /// </summary> /// <param name="item"></param> public void DeleteItemDesc(ItemDesc item) { try { DataAccess.ExecuteNonQuery(DataSQL.DeleteItemDesc(item.ItemCode)); } catch (Exception ex) { //Throw a custom-trace exception throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
/// <summary> /// Event handler for all button clicks /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click(object sender, RoutedEventArgs e) { try { //Get the button that was pressed Button btn = sender as Button; //Execute action based on the button pressed //Save button if (sender.Equals(btnSave)) { ItemDesc item; string oldItemCode; //Check if it's an add or edit if (isNewItem) { item = new ItemDesc(); oldItemCode = "NULL"; } else { item = dgItemDescs.SelectedItem as ItemDesc; oldItemCode = item.ItemCode; } //Set the information from the text boxes to the item if (!txtItemCode.Text.Equals("")) { item.ItemCode = txtItemCode.Text; txtItemCode.BorderBrush = Brushes.DarkBlue; } else { txtItemCode.BorderBrush = Brushes.Red; return; } if (!txtDescription.Text.Equals("")) { item.Description = txtDescription.Text; txtDescription.BorderBrush = Brushes.DarkBlue; } else { txtDescription.BorderBrush = Brushes.Red; return; } if (double.TryParse(txtCost.Text, out double result)) { item.Cost = result; txtCost.BorderBrush = Brushes.DarkBlue; } else { txtCost.BorderBrush = Brushes.Red; return; } //Add or update to the database if (isNewItem) { if (!dataMaster.AddNewItemDesc(item)) { MessageBox.Show("Error: Duplicate Item Code.", "Duplicate Item Code", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else { dataMaster.UpdateItemDesc(item); } //Refresh items list dgItemDescs.ItemsSource = dataMaster.MakeItemDescList(); DisableItemEditMode(); } //Cancel button else if (sender.Equals(btnCancel)) { //refresh item list and disable edit mode dgItemDescs.ItemsSource = dataMaster.MakeItemDescList(); DisableItemEditMode(); } //Edit Item button else if (sender.Equals(btnEdit)) { //The datagrid, addeditdelete buttons should be locked, //Unlock savecancel buttons, //Set textboxes Readonly to false if (dgItemDescs.SelectedIndex != -1) { isNewItem = false; EnableItemEditMode(); } } //Add New Item button else if (sender.Equals(btnAdd)) { //enable edit mode for new item isNewItem = true; EnableItemEditMode(); } //Delete Item button else if (sender.Equals(btnDelete)) { //Confirm deletion of the item if (dgItemDescs.SelectedIndex != -1) { MessageBoxResult result = MessageBox.Show("Delete this item?", "Confirm Deletion", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { dataMaster.DeleteItemDesc(dgItemDescs.SelectedItem as ItemDesc); dgItemDescs.ItemsSource = dataMaster.MakeItemDescList(); } } } //Close button else { Close(); } } catch (Exception ex) { if (ex.Message.Contains("related records")) { MessageBox.Show("Cannot Delete Item, because it is actively used in an invoice.", "Delete Item Denied", MessageBoxButton.OK, MessageBoxImage.Error); } else { //Throw a custom-trace exception throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } } }