private void TambahData() { if (tbUserID.Text != "" || tbPassword.Text != "") { try { // ini kodenya, yg catch buat tahu salahnya apa using (var db = new DBUserModel()) { UserDB newUser = new UserDB { userID = tbUserID.Text, password = tbPassword.Text }; db.UserDBs.Add(newUser); db.SaveChanges(); MessageBox.Show("User successfully added"); Close(); } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } } }
private void pbLogin_Click(object sender, EventArgs e) { bool Verify = false; using (var db = new DBUserModel()) { var query = from UserDB in db.UserDBs where UserDB.userID == tbUserID.Text && UserDB.password == tbPassword.Text select UserDB; foreach (var item in query) { User user1 = new User(item.userID, item.password); FormMain mainForm = new FormMain(tbUserID.Text); mainForm.Show(); Verify = true; } } if (Verify) { MessageBox.Show("Login success"); } else { MessageBox.Show("UserID and Password is not registered"); } }