//Clearing all texbox in tab supplier private void Clear_Textbox_Supplier() { Tbx_Id.Clear(); Tbx_Name.Clear(); Tbx_Address.Clear(); Tbx_Email.Clear(); Tbx_Search.Clear(); }
//Code for send password button private void Button_Click(object sender, RoutedEventArgs e) { var check_email = connection.Users.Where(S => S.Email == Tbx_Email.Text).FirstOrDefault(); //get the data first data from database which match the id if (check_email == null) { MessageBox.Show("Your email is wrong!"); Tbx_Email.Focus(); //Move cursor to text box Name return; //nothing will happen } var password = Guid.NewGuid().ToString(); //generate password with guid check_email.Password = password; // changes the password with new password connection.SaveChanges(); MailMessage mm = new MailMessage("*****@*****.**", check_email.Email); string today = DateTime.Now.ToString(); mm.Subject = "Password Recovery (" + today + ")"; mm.Body = string.Format("Hi {0},<br /><br />Your password has been changed to: <br />{1}<br /><br />Thank You.", check_email.Email, password); mm.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; //Definition of sender System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); NetworkCred.UserName = "******"; NetworkCred.Password = "******"; smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; try { smtp.Send(mm); } catch { } MessageBox.Show("New password has been sent to your email!"); Login login = new Login(); login.Show(); this.Close(); }
//Code for login button private void Button_Click(object sender, RoutedEventArgs e) { var check_email = connection.Users.Where(S => S.Email == Tbx_Email.Text).FirstOrDefault(); //get the data first data from database which match the id if (check_email == null) { MessageBox.Show("Your email is wrong!"); Tbx_Email.Focus(); //Move cursor to text box Name return; //nothing will happen } else if (check_email.Password != Tbx_Password.Password.ToString()) { MessageBox.Show("Your password is wrong!"); Tbx_Password.Focus(); //Move cursor to text box Name return; //nothing will happen } MainWindow main = new MainWindow(check_email.Email); main.Show(); this.Close(); }
// 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 }