/// <summary> /// Adds user to user table when new user registers /// </summary> /// <param name="user"></param> public static void AddUser(Users user) { string userName; SqlConnection connection = BrainFartConnection.GetConnection(); string insertStatement = "Insert into users " + "(username, password) " + "Values (@UserName, @userPassword)"; SqlCommand insertCommand = new SqlCommand(insertStatement, connection); insertCommand.Parameters.AddWithValue("@UserName", user.UserName); insertCommand.Parameters.AddWithValue("@UserPassword", user.UserPassword); userName = user.UserName; try { connection.Open(); insertCommand.ExecuteNonQuery(); string selectStatement = "SELECT IDENT_CURRENT('Users') FROM Users"; SqlCommand selectCommand = new SqlCommand(selectStatement, connection); int userID = Convert.ToInt32(selectCommand.ExecuteScalar()); } catch (SqlException ex) { throw ex; } catch (Exception e) { throw e; } finally { if (connection != null) connection.Close(); } }
private void btnRegister_Click(object sender, EventArgs e) { if (IsValidData()) { user = new Users(); this.getUserData(user); this.confirmPassword(); } }
/// <summary> /// Logins User /// </summary> /// <param name="username">User name</param> /// <returns></returns> public static Users UserLogin(string username, Users LogonUser) { string SelectStatement = string.Empty; SelectStatement = "Select userID, username, password from Users " + " where username like @UserName"; SqlConnection connection = BrainFartConnection.GetConnection(); SqlCommand selectCommand = new SqlCommand(SelectStatement, connection); selectCommand.Parameters.AddWithValue("@UserName", username); SqlDataReader reader = null; try { connection.Open(); reader = selectCommand.ExecuteReader(); while (reader.Read()) { LogonUser.UserID = (int)reader["userID"]; LogonUser.UserName = reader["userName"].ToString(); LogonUser.UserPassword = reader["passWord"].ToString(); } } catch (SqlException ex) { throw ex; } catch (Exception ex1) { throw ex1; } finally { if (connection != null) connection.Close(); if (reader != null) reader.Close(); } return LogonUser; }
private void getUserData(Users user) { user.UserName = txtUserName.Text; user.UserPassword = txtPassword.Text; user.PasswordConfirm = txtConfirm.Text; }
/// <summary> /// Logs out a user. This method simply assigns null to the logged user /// logged date and user role objects. /// </summary> internal void LogOutUser() { currentLoggedUser = null; loggedInDateTime = null; }
/// <summary> /// Logs in a user to the system and assign the logged user and logged in date and time /// </summary> /// <param name="username">user name</param> /// <param name="password">password</param> /// <returns></returns> internal bool LoginUser(string username, string password) { bool UserLoginSuccess = false; currentLoggedUser = new Users(); UserAccessDAL.UserLogin(username, currentLoggedUser); if (currentLoggedUser.UserPassword == password) { UserLoginSuccess = true; loggedInDateTime = DateTime.Now; } return UserLoginSuccess; }