private void Tbx_Name_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) { if (!del_event) { if (Tbx_Name.Text == "") { MessageBox.Show("Please enter item name"); Tbx_Name.Focus(); } } }
// code for save button supplier private void Btn_Save_Click(object sender, RoutedEventArgs e) { var check_exist = connection.Users.Where(S => S.Email == Tbx_Email.Text).FirstOrDefault(); if (Tbx_Name.Text.Trim() == string.Empty) //if textbox name empty { MessageBox.Show("Please fill supplier Name!"); Tbx_Name.Focus(); //Move cursor to text box Name return; //nothing will happen } else if (Tbx_Address.Text.Trim() == string.Empty) // if textbox address empty { MessageBox.Show("Please fill supplier Address!"); Tbx_Address.Focus(); //Move cursor to text box Name return; //nothing will happen } else if (Tbx_Email.Text.Trim() == string.Empty) // if textbox email empty { MessageBox.Show("Please fill supplier Email!"); Tbx_Address.Focus(); //Move cursor to text box Name return; //nothing will happen } //Validation Character else if (System.Text.RegularExpressions.Regex.IsMatch(Tbx_Email.Text, "[^a-zA-Z0-9@._]")) { MessageBox.Show("Email format wrong!"); Tbx_Email.Focus(); return; } else if (!Tbx_Email.Text.Contains("@gmail.com") && !Tbx_Email.Text.Contains("@yahoo.com")) { MessageBox.Show("Email format wrong!"); Tbx_Email.Focus(); return; } // Update Code if (Tbx_Id.Text != "") { // Update Supplier int Id = Convert.ToInt32(Tbx_Id.Text); //get id from textbox id and convert to int var check_id = connection.Suppliers.Where(S => S.Id == Id).FirstOrDefault(); //get the data first data from database which match the id check_id.Name = Tbx_Name.Text; //assign the name with value from textbox name check_id.Address = Tbx_Address.Text; //assign the address with value from textbox address check_id.Email = Tbx_Email.Text; //assign the email with value from textbox email connection.SaveChanges(); //if not empty then update the database // Update User var checkEmail = connection.Suppliers.Where(S => S.Email == Tbx_Email.Text).FirstOrDefault(); var success = connection.SaveChanges(); MessageBox.Show(success + " Data Successfully Updated!"); } // Insert Code else { //Validation Exist if (check_exist != null) { MessageBox.Show("Email already used!"); Tbx_Email.Focus(); return; } // Add Supplier var input = new Supplier(Tbx_Name.Text, Tbx_Address.Text, Tbx_Email.Text); //get user input from textbox connection.Suppliers.Add(input); // if not empty then add input connection.SaveChanges(); // Add User var password = Guid.NewGuid().ToString(); //generate password with guid var input2 = new User(Tbx_Email.Text, password, "member"); connection.Users.Add(input2); var success = connection.SaveChanges(); Send_Password(Tbx_Email.Text, password); //send password to email MessageBox.Show(success + " Data Successfully Inputted & Password Has Been Sent to Email!"); } TB_M_Supplier.ItemsSource = connection.Suppliers.ToList(); //refresh the datagrid Cb_Supplier.ItemsSource = connection.Suppliers.ToList(); // refresh combo box item Clear_Textbox_Supplier(); //Clear all text box }