//Code for save button item private void Btn_Save_Item_Click(object sender, RoutedEventArgs e) { var supplierdata = connection.Suppliers.Where(S => S.Id == cb_sup).FirstOrDefault(); //search the supplier data that match the supplier id if (Tbx_Name_Item.Text.Trim() == string.Empty) //if textbox name empty { MessageBox.Show("Please fill item Name!"); Tbx_Name_Item.Focus(); //Move cursor to text box Name return; //nothing will happen } else if (Tbx_Price.Text.Trim() == string.Empty) // if textbox Price empty { MessageBox.Show("Please fill item Price!"); Tbx_Price.Focus(); //Move cursor to text box Name return; //nothing will happen } else if (Tbx_Stock.Text.Trim() == string.Empty) // if textbox Stock empty { MessageBox.Show("Please fill item Stock!"); Tbx_Stock.Focus(); //Move cursor to text box Name return; //nothing will happen } else if (supplierdata == null) { MessageBox.Show("Please choose Supplier!"); Cb_Supplier.Focus(); //Move cursor to text box Name return; //nothing will happen } else if (System.Text.RegularExpressions.Regex.IsMatch(Tbx_Name_Item.Text, "[^a-zA-Z0-9]")) { MessageBox.Show("Item name format wrong!"); Tbx_Name_Item.Focus(); return; } // Update Code if (Tbx_Id_Item.Text != "") { int Id = Convert.ToInt32(Tbx_Id_Item.Text); //get id from textbox id and convert to int var check_id = connection.Items.Where(S => S.Id == Id).FirstOrDefault(); //get the data first data from database which match the id check_id.Name = Tbx_Name_Item.Text; check_id.Price = Convert.ToInt32(Tbx_Price.Text); check_id.Stock = Convert.ToInt32(Tbx_Stock.Text); check_id.Supplier = supplierdata; var success = connection.SaveChanges(); //update the database (add the input to database) MessageBox.Show(success + " Data Successfully Updated!"); } // Insert Code else { var input = new Item(Tbx_Name_Item.Text, Convert.ToInt32(Tbx_Price.Text), Convert.ToInt32(Tbx_Stock.Text), supplierdata); //get user input from textbox connection.Items.Add(input); // if not empty then add input var success = connection.SaveChanges(); //update the database (add the input to database) MessageBox.Show(success + " Data Successfully Inputted!"); } TB_M_Item.ItemsSource = connection.Items.ToList(); // refresh datagrid item Cb_Supplier.ItemsSource = connection.Suppliers.ToList(); //refresh Clear_Textbox_Item(); //Clear all text box }
//Clear all textbox in Item private void Clear_Textbox_Item() { Tbx_Id_Item.Clear(); Tbx_Name_Item.Clear(); Tbx_Price.Clear(); Tbx_Stock.Clear(); Cb_Supplier.SelectedValue = null; Tbx_Search2.Clear(); }