private void btnLogin_Click(object sender, EventArgs e) { errProvider.Clear(); string userName = txtUserName.Text; string password = txtPassword.Text; UserService userService = new UserService(); bool isUserExisted = userService.IsNameExisted(userName); if (isUserExisted == false) { errProvider.SetError(txtUserName, "UserName has not existed, please try it again"); return; } User user = userService.GetUserByNameForLogin(userName); if (user.Password == password) { int userId = user.UserId; UserLogined.UserId = userId; UserLogined.UserName = user.Name; UserGroupService userGroupService = new UserGroupService(); UserGroup group = userGroupService.GetUserGroupByUserId(userId); if (group != null) { UserLogined.GroupId = group.GroupId; UserLogined.GroupName = group.Group.Name; } Thread thread = new Thread(OpenMain); thread.Start(); this.Close(); this.Dispose(); } else { errProvider.SetError(txtPassword, "UserName and Password are not matched, please try it again"); } }