/// <summary> /// Checks first and last name against ds_User to see if it already exists. /// Presents dialog asking to confirm if duplicate. /// </summary> /// <param name="mdl_User"></param> /// <param name="tbl_User"></param> /// <returns>True if confirmed that user already does exist in database, False if new user</returns> public bool userExists(mdl_User mdl_User, DataTable tbl_User) { //bool userExists = false; var existingUser = from row in tbl_User.AsEnumerable() where row.Field <string>("FirstName").ToLower() == mdl_User.FirstName.ToLower() && row.Field <string>("LastName").ToLower() == mdl_User.LastName.ToLower() select row; if (existingUser.Count() > 0) { string existingMessage = $"Is this a duplicate of an existing user?" + Environment.NewLine + Environment.NewLine; foreach (DataRow user in existingUser) { existingMessage += $"{user.Field<string>("FirstName")} " + $"{user.Field<string>("LastName")}, " + $"{user.Field<string>("Email")}" + Environment.NewLine + $"{user.Field<string>("Organisation")}" + Environment.NewLine; } DialogResult confirm = MessageBox.Show( text: existingMessage , caption: "Existing user?" , buttons: MessageBoxButtons.YesNo); if (confirm == DialogResult.Yes) { return(true); } } return(false); }
/// <summary> /// Takes the UserModel and constructs a string that is presented via a MessageBox for /// review of entered details. Returns which button was clicked. /// </summary> /// <param name="mdl_User"></param> /// <returns></returns> private DialogResult confirmationBox(mdl_User mdl_User) { // tabs and newlines align and break up displayed information for ease of review string reviewUserDetails = $"Create new user with these details?" + Environment.NewLine + Environment.NewLine; reviewUserDetails += $"Status:\t\t{mdl_User.Status_Desc}" + Environment.NewLine + Environment.NewLine; reviewUserDetails += $"Title:\t\t{mdl_User.Title_Desc}" + Environment.NewLine; reviewUserDetails += $"FirstName:\t{mdl_User.FirstName}" + Environment.NewLine; reviewUserDetails += $"LastName:\t{mdl_User.LastName}" + Environment.NewLine + Environment.NewLine; reviewUserDetails += $"Email:\t\t{mdl_User.Email}" + Environment.NewLine; reviewUserDetails += $"Phone:\t\t{mdl_User.Phone}" + Environment.NewLine; reviewUserDetails += $"UserName:\t{mdl_User.UserName}" + Environment.NewLine; reviewUserDetails += $"Organisation:\t{mdl_User.Organisation}" + Environment.NewLine + Environment.NewLine; reviewUserDetails += $"StartDate:\t\t{mdl_User.StartDate}" + Environment.NewLine; reviewUserDetails += $"EndDate:\t\t{mdl_User.EndDate}" + Environment.NewLine + Environment.NewLine; reviewUserDetails += $"IRCAgreement:\t{mdl_User.IRCAgreement}" + Environment.NewLine; reviewUserDetails += $"ISET:\t\t{mdl_User.ISET}" + Environment.NewLine; reviewUserDetails += $"ISAT:\t\t{mdl_User.ISAT}" + Environment.NewLine; reviewUserDetails += $"SAFE:\t\t{mdl_User.SAFE}" + Environment.NewLine + Environment.NewLine; reviewUserDetails += $"TokenSerial:\t{mdl_User.TokenSerial}" + Environment.NewLine; reviewUserDetails += $"TokenIssued:\t{mdl_User.TokenIssued}" + Environment.NewLine; reviewUserDetails += $"TokenReturned:\t{mdl_User.TokenReturned}" + Environment.NewLine; DialogResult confirm = MessageBox.Show( text: reviewUserDetails , caption: "Confirmation" , buttons: MessageBoxButtons.OKCancel); return(confirm); }
/// <summary> /// Method to assign values to class variables that hold current user details. /// Creates new User class object and uses parameter UserNumber with class DataSet (ds_User) to /// populate UserModel (mdl)currentUser) using method from User class: getUser(UserNumber, ds_User). /// </summary> /// <param name="UserNumber"></param> private void fillCurrentUserVariables(int UserNumber) { try { //instantiate new User type object that contains User methods var User = new User(); //populate class variables of UserModel mdl_CurrentUser = User.getUser(UserNumber, ds_User); } catch (Exception ex) { MessageBox.Show("Method fillCurrentProjectVariables of class frm_Projects has failed" + Environment.NewLine + Environment.NewLine + ex); //throw; } }
/// <summary> /// Checks if mandatory fields have an entry. Returns false and feedback via messagebox in case of absence. /// </summary> /// <param name="mdl_User"></param> /// <returns></returns> public bool requiredFields(mdl_User mdl_User) { bool requiredFields = true; //Check required fields have an entry if (requiredFields == true & string.IsNullOrWhiteSpace(mdl_User.FirstName)) { MessageBox.Show("Please enter a First Name."); requiredFields = false; } if (requiredFields == true & string.IsNullOrWhiteSpace(mdl_User.LastName)) { MessageBox.Show("Please enter a Last Name."); requiredFields = false; } if (requiredFields == true & string.IsNullOrWhiteSpace(mdl_User.Email)) { MessageBox.Show("Please enter a Email Address."); requiredFields = false; } return(requiredFields); }
/// <summary> /// Takes values from form controls, checks dates are dates, calls confirmationBox /// to present them for review and inserts into SQL database if confirmed. /// </summary> private bool insertNewUser() { User Users = new User(); mdl_User mdl_User = new mdl_User(); //put control values into UserModel mdl_User.UserNumber = Users.getLastUserNumber() + 1; if (cb_UserStatus.SelectedIndex > -1) { mdl_User.Status = int.Parse(cb_UserStatus.SelectedValue.ToString()); mdl_User.Status_Desc = cb_UserStatus.Text; } if (cb_UserTitle.SelectedIndex > -1) { mdl_User.Title = int.Parse(cb_UserTitle.SelectedValue.ToString()); mdl_User.Title_Desc = cb_UserTitle.Text; } mdl_User.FirstName = tb_FirstName.Text; mdl_User.LastName = tb_LastName.Text; mdl_User.Email = tb_Email.Text; mdl_User.Phone = tb_Phone.Text; mdl_User.UserName = tb_UserName.Text; mdl_User.Organisation = tb_Organisation.Text; if (nud_TokenSerial.Value > 0) { mdl_User.TokenSerial = (long)nud_TokenSerial.Value; } // Dates are fuckey bool dateCheck = true; if (dateCheck == true & mtb_UserStartDate.Text != "" & mtb_UserStartDate.Text != " / /") { try { mdl_User.StartDate = Convert.ToDateTime(mtb_UserStartDate.Text); } catch (Exception) { MessageBox.Show("Please enter valid Start Date."); dateCheck = false; } } if (dateCheck == true & mtb_UserEndDate.Text != "" & mtb_UserEndDate.Text != " / /") { try { mdl_User.EndDate = Convert.ToDateTime(mtb_UserEndDate.Text); } catch (Exception) { MessageBox.Show("Please enter valid End Date."); dateCheck = false; } } if (dateCheck == true & mtb_IRCAgreement.Text != "" & mtb_IRCAgreement.Text != " / /") { try { mdl_User.IRCAgreement = Convert.ToDateTime(mtb_IRCAgreement.Text); } catch (Exception) { MessageBox.Show("Please enter valid IRC Agreement Date."); dateCheck = false; } } if (dateCheck == true & mtb_ISET.Text != "" & mtb_ISET.Text != " / /") { try { mdl_User.ISET = Convert.ToDateTime(mtb_ISET.Text); } catch (Exception) { MessageBox.Show("Please enter valid ISET Date."); dateCheck = false; } } if (dateCheck == true & mtb_ISAT.Text != "" & mtb_ISAT.Text != " / /") { try { mdl_User.ISAT = Convert.ToDateTime(mtb_ISAT.Text); } catch (Exception) { MessageBox.Show("Please enter valid ISAT Date."); dateCheck = false; } } if (dateCheck == true & mtb_SAFE.Text != "" & mtb_SAFE.Text != " / /") { try { mdl_User.SAFE = Convert.ToDateTime(mtb_SAFE.Text); } catch (Exception) { MessageBox.Show("Please enter valid SAFE Date."); dateCheck = false; } } if (dateCheck == true & mtb_TokenIssued.Text != "" & mtb_TokenIssued.Text != " / /") { try { mdl_User.TokenIssued = Convert.ToDateTime(mtb_TokenIssued.Text); } catch (Exception) { MessageBox.Show("Please enter valid Token Issued Date."); dateCheck = false; } } if (dateCheck == true & mtb_TokenReturned.Text != "" & mtb_TokenReturned.Text != " / /") { try { mdl_User.TokenReturned = Convert.ToDateTime(mtb_TokenReturned.Text); } catch (Exception) { MessageBox.Show("Please enter valid Token Returned Date."); dateCheck = false; } } //Check required fields have an entry if (Users.requiredFields(mdl_User) == false) { return(false); } //Check if user already exists if (Users.userExists(mdl_User, ds_User.Tables["tblUser"]) == true) { MessageBox.Show("New user not added."); return(false); } if (dateCheck == true) { if (confirmationBox(mdl_User) == DialogResult.OK) { //insert new record if (Users.insertUser(mdl_User) == true) { return(true); } } } return(false); }
/// <summary> /// Method to populate UserModel with latest single record. /// Uses parameter UserNumber to query User DataSet (passed as parameter ds_User), assigns /// values to the returned UserModel class variables. /// </summary> /// <param name="UserNumber"></param> /// <param name="ds_User"></param> /// <returns></returns> public mdl_User getUser(int UserNumber, DataSet ds_User) { mdl_User mdl_User = new mdl_User(); //if no records found, try will fail at "DataRow uRow = uRows[i];" and go to catch try { DataRow[] uRows = ds_User.Tables["tblUser"].Select($"UserNumber = '{UserNumber}'"); //there's always a small a chance a user might have multiple records where ValidTo is null //DataSet (ds_usr) is populated ordered by names and then UserID so largest (last added) UserID for each user is last //feed back to operator if more than one 'current' project record if (uRows.Count() > 1) { MessageBox.Show("More than one current record found for this user, showing last only. Please contact a system administrator."); } //active row (uRow) filled with last row from uRows int i = uRows.Count() - 1; DataRow uRow = uRows[i]; //populate DataRow to output with values from uRow mdl_User.UserID = (int)uRow["UserID"]; mdl_User.UserNumber = (int)uRow["UserNumber"]; if (uRow["Status"].ToString().Length > 0) { mdl_User.Status = (int?)uRow["Status"]; } if (uRow["Title"].ToString().Length > 0) { mdl_User.Title = (int?)uRow["Title"]; } mdl_User.FirstName = uRow["FirstName"].ToString(); mdl_User.LastName = uRow["LastName"].ToString(); mdl_User.Email = uRow["Email"].ToString(); mdl_User.Phone = uRow["Phone"].ToString(); mdl_User.UserName = uRow["UserName"].ToString(); mdl_User.Organisation = uRow["Organisation"].ToString(); if (uRow["StartDate"].ToString().Length > 0) { mdl_User.StartDate = (DateTime?)uRow["StartDate"]; } if (uRow["EndDate"].ToString().Length > 0) { mdl_User.EndDate = (DateTime?)uRow["EndDate"]; } mdl_User.Priviledged = (bool)uRow["Priviledged"]; if (uRow["SEEDAgreement"].ToString().Length > 0) { mdl_User.SEEDAgreement = (DateTime?)uRow["SEEDAgreement"]; } if (uRow["IRCAgreement"].ToString().Length > 0) { mdl_User.IRCAgreement = (DateTime?)uRow["IRCAgreement"]; } if (uRow["LASERAgreement"].ToString().Length > 0) { mdl_User.LASERAgreement = (DateTime?)uRow["LASERAgreement"]; } if (uRow["ISET"].ToString().Length > 0) { mdl_User.ISET = (DateTime?)uRow["ISET"]; } if (uRow["ISAT"].ToString().Length > 0) { mdl_User.ISAT = (DateTime?)uRow["ISAT"]; } if (uRow["SAFE"].ToString().Length > 0) { mdl_User.SAFE = (DateTime?)uRow["SAFE"]; } if (uRow["TokenSerial"].ToString().Length > 0) { mdl_User.TokenSerial = (long?)uRow["TokenSerial"]; } if (uRow["TokenIssued"].ToString().Length > 0) { mdl_User.TokenIssued = (DateTime?)uRow["TokenIssued"]; } if (uRow["TokenReturned"].ToString().Length > 0) { mdl_User.TokenReturned = (DateTime?)uRow["TokenReturned"]; } } catch (Exception ex) { MessageBox.Show("Failed to load user details" + Environment.NewLine + Environment.NewLine + ex); throw; } return(mdl_User); }
/// <summary> /// Method to insert a new user record into dbo.tblUser. /// Takes UserModel class as parameter, adds class member variables to a SQL query string /// as parameters then executes an insert. /// Returns a boolean true on success, defaults to false /// </summary> /// <param name="mdl_User"></param> /// <returns></returns> public bool insertUser(mdl_User mdl_User) { bool success = false; try { SqlConnection conn = new SqlConnection(); conn.ConnectionString = SQL_Stuff.conString; conn.Credential = SQL_Stuff.credential; using (conn) { //generate the parameterised SQL query to insert new record SqlCommand qryInsertUser = new SqlCommand(); qryInsertUser.Connection = conn; qryInsertUser.CommandText = "insert into [dbo].[tblUser] " + "([UserNumber], [Status], [Title], [FirstName], [LastName], [Email], [Phone], [UserName]" + ", [Organisation], [StartDate], [EndDate], [SEEDAgreement], [IRCAgreement], [LASERAgreement]" + ", [ISET], [ISAT], [SAFE], [TokenSerial], [TokenIssued], [TokenReturned]) " + "values " + "(@UserNumber, @Status, @Title, @FirstName, @LastName, @Email, @Phone, @UserName" + ", @Organisation, @StartDate, @EndDate, @SEEDAgreement, @IRCAgreement, @LASERAgreement" + ", @ISET, @ISAT, @SAFE, @TokenSerial, @TokenIssued, @TokenReturned)"; //assign the parameter values qryInsertUser.Parameters.Add("@UserNumber", SqlDbType.Int).Value = mdl_User.UserNumber; SqlParameter param_Status = new SqlParameter("@Status", mdl_User.Status == null ? (object)DBNull.Value : mdl_User.Status); param_Status.IsNullable = true; qryInsertUser.Parameters.Add(param_Status); SqlParameter param_Title = new SqlParameter("@Title", mdl_User.Title == null ? (object)DBNull.Value : mdl_User.Title); param_Title.IsNullable = true; qryInsertUser.Parameters.Add(param_Title); qryInsertUser.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = mdl_User.FirstName; qryInsertUser.Parameters.Add("@LastName", SqlDbType.VarChar, 50).Value = mdl_User.LastName; qryInsertUser.Parameters.Add("@Email", SqlDbType.VarChar, 255).Value = mdl_User.Email; qryInsertUser.Parameters.Add("@Phone", SqlDbType.VarChar, 15).Value = mdl_User.Phone; qryInsertUser.Parameters.Add("@UserName", SqlDbType.VarChar, 12).Value = mdl_User.UserName; qryInsertUser.Parameters.Add("@Organisation", SqlDbType.VarChar, 255).Value = mdl_User.Organisation; SqlParameter param_StartDate = new SqlParameter("@StartDate", mdl_User.StartDate == null ? (object)DBNull.Value : mdl_User.StartDate); param_StartDate.IsNullable = true; qryInsertUser.Parameters.Add(param_StartDate); SqlParameter param_EndDate = new SqlParameter("@EndDate", mdl_User.EndDate == null ? (object)DBNull.Value : mdl_User.EndDate); param_EndDate.IsNullable = true; qryInsertUser.Parameters.Add(param_EndDate); SqlParameter param_SEEDAgreement = new SqlParameter("@SEEDAgreement", mdl_User.SEEDAgreement == null ? (object)DBNull.Value : mdl_User.SEEDAgreement); param_SEEDAgreement.IsNullable = true; qryInsertUser.Parameters.Add(param_SEEDAgreement); SqlParameter param_IRCAgreement = new SqlParameter("@IRCAgreement", mdl_User.IRCAgreement == null ? (object)DBNull.Value : mdl_User.IRCAgreement); param_IRCAgreement.IsNullable = true; qryInsertUser.Parameters.Add(param_IRCAgreement); SqlParameter param_LASERAgreement = new SqlParameter("@LASERAgreement", mdl_User.LASERAgreement == null ? (object)DBNull.Value : mdl_User.LASERAgreement); param_LASERAgreement.IsNullable = true; qryInsertUser.Parameters.Add(param_LASERAgreement); SqlParameter param_ISET = new SqlParameter("@ISET", mdl_User.ISET == null ? (object)DBNull.Value : mdl_User.ISET); param_ISET.IsNullable = true; qryInsertUser.Parameters.Add(param_ISET); SqlParameter param_ISAT = new SqlParameter("@ISAT", mdl_User.ISAT == null ? (object)DBNull.Value : mdl_User.ISAT); param_ISAT.IsNullable = true; qryInsertUser.Parameters.Add(param_ISAT); SqlParameter param_SAFE = new SqlParameter("@SAFE", mdl_User.SAFE == null ? (object)DBNull.Value : mdl_User.SAFE); param_SAFE.IsNullable = true; qryInsertUser.Parameters.Add(param_SAFE); SqlParameter param_TokenSerial = new SqlParameter("@TokenSerial", mdl_User.TokenSerial == null ? (object)DBNull.Value : mdl_User.TokenSerial); param_TokenSerial.IsNullable = true; qryInsertUser.Parameters.Add(param_TokenSerial); SqlParameter param_TokenIssued = new SqlParameter("@TokenIssued", mdl_User.TokenIssued == null ? (object)DBNull.Value : mdl_User.TokenIssued); param_TokenIssued.IsNullable = true; qryInsertUser.Parameters.Add(param_TokenIssued); SqlParameter param_TokenReturned = new SqlParameter("@TokenReturned", mdl_User.TokenReturned == null ? (object)DBNull.Value : mdl_User.TokenReturned); param_TokenReturned.IsNullable = true; qryInsertUser.Parameters.Add(param_TokenReturned); //open connection to database, run query and close connection conn.Open(); qryInsertUser.ExecuteNonQuery(); MessageBox.Show($"User details updated for {mdl_User.LastName}, {mdl_User.FirstName}"); success = true; } } catch (Exception ex) { MessageBox.Show("Failed to insert new project record" + Environment.NewLine + Environment.NewLine + ex); } return(success); }
/// <summary> /// Method to update user details in SQL Server database. /// Gets values from form, assigns them to member variables of new UserModel (mdl_NewUser). /// Compares new UserModel against current User Model and hecks that dates are valid. /// If no difference then no action, if invalid dates then no action and MessageBox feedback. /// Creates new User class object and first checks if record loaded to form is latest user record /// in database, using parameter UserNumber and current user UserID. /// If not latest record then no action and MessageBox feedback (asking to refresh form and try again). /// Inserts new record to database using insertUser method from User class and values contained in /// new user model. /// Logically deletes current record in database using deleteUser method from User class. /// Refreshes the class DataSet (ds_User), assigns new values to current user model (mdl_CurrentUser) and /// resets form controls in the same manner as at form load. /// </summary> /// <param name="UserNumber"></param> private bool updateUser(int UserNumber) { bool success = false; mdl_User mdl_NewUser = new mdl_User(); mdl_NewUser.UserNumber = UserNumber; if (cb_UserStatus.SelectedIndex > -1) { mdl_NewUser.Status = int.Parse(cb_UserStatus.SelectedValue.ToString()); } if (cb_UserTitle.SelectedIndex > -1) { mdl_NewUser.Title = int.Parse(cb_UserTitle.SelectedValue.ToString()); } mdl_NewUser.FirstName = tb_FirstName.Text; mdl_NewUser.LastName = tb_LastName.Text; mdl_NewUser.Email = tb_Email.Text; mdl_NewUser.Phone = tb_Phone.Text; mdl_NewUser.UserName = tb_UserName.Text; mdl_NewUser.Organisation = tb_Organisation.Text; if (nud_TokenSerial.Value > 0) { mdl_NewUser.TokenSerial = (int)(nud_TokenSerial.Value); } //dates are fuckey bool dateCheck = true; if (dateCheck == true & mtb_UserStartDate.Text != "" & mtb_UserStartDate.Text != " / /") { try { mdl_NewUser.StartDate = Convert.ToDateTime(mtb_UserStartDate.Text); } catch (Exception) { MessageBox.Show("Please enter valid User Start Date"); dateCheck = false; } } if (dateCheck == true & mtb_UserEndDate.Text != "" & mtb_UserEndDate.Text != " / /") { try { mdl_NewUser.EndDate = Convert.ToDateTime(mtb_UserEndDate.Text); } catch (Exception) { MessageBox.Show("Please enter valid User End Date"); dateCheck = false; } } if (dateCheck == true & mtb_SEEDAgreement.Text != "" & mtb_SEEDAgreement.Text != " / /") { try { mdl_NewUser.SEEDAgreement = Convert.ToDateTime(mtb_SEEDAgreement.Text); } catch (Exception) { MessageBox.Show("Please enter valid SEED Confidentiality Agreement Date"); dateCheck = false; } } if (dateCheck == true & mtb_IRCAgreement.Text != "" & mtb_IRCAgreement.Text != " / /") { try { mdl_NewUser.IRCAgreement = Convert.ToDateTime(mtb_IRCAgreement.Text); } catch (Exception) { MessageBox.Show("Please enter valid IRC User Agreement Date"); dateCheck = false; } } if (dateCheck == true & mtb_LASERAgreement.Text != "" & mtb_LASERAgreement.Text != " / /") { try { mdl_NewUser.LASERAgreement = Convert.ToDateTime(mtb_LASERAgreement.Text); } catch (Exception) { MessageBox.Show("Please enter valid IRC User Agreement Date"); dateCheck = false; } } if (dateCheck == true & mtb_ISET.Text != "" & mtb_ISET.Text != " / /") { try { mdl_NewUser.ISET = Convert.ToDateTime(mtb_ISET.Text); } catch (Exception) { MessageBox.Show("Please enter valid ISET Date"); dateCheck = false; } } if (dateCheck == true & mtb_ISAT.Text != "" & mtb_ISAT.Text != " / /") { try { mdl_NewUser.ISAT = Convert.ToDateTime(mtb_ISAT.Text); } catch (Exception) { MessageBox.Show("Please enter valid ISAT Date"); dateCheck = false; } } if (dateCheck == true & mtb_SAFE.Text != "" & mtb_SAFE.Text != " / /") { try { mdl_NewUser.SAFE = Convert.ToDateTime(mtb_SAFE.Text); } catch (Exception) { MessageBox.Show("Please enter valid SAFE Date"); dateCheck = false; } } if (dateCheck == true & mtb_TokenIssued.Text != "" & mtb_TokenIssued.Text != " / /") { try { mdl_NewUser.TokenIssued = Convert.ToDateTime(mtb_TokenIssued.Text); } catch (Exception) { MessageBox.Show("Please enter valid Token Issued Date"); dateCheck = false; } } if (dateCheck == true & mtb_TokenReturned.Text != "" & mtb_TokenReturned.Text != " / /") { try { mdl_NewUser.TokenReturned = Convert.ToDateTime(mtb_TokenReturned.Text); } catch (Exception) { MessageBox.Show("Please enter valid Token Returned Date"); dateCheck = false; } } //instantiate new User type object that contains methods to update db User Users = new User(); //Check required fields have an entry if (Users.requiredFields(mdl_NewUser) == false) { return(success = false); } //check to see if any changes have been made, no need to update if none. if (mdl_CurrentUser == mdl_NewUser) { return(success = true); } if (dateCheck == true) { //check that record currently displayed is current record in database before updating anything if (Users.checkCurrentRecord(UserNumber, mdl_CurrentUser.UserID) == true) { //update existing user - first perform insert new record, if success returned = true then logical delete if (Users.insertUser(mdl_NewUser)) { Users.deleteUser(mdl_CurrentUser.UserID); } //refresh dataset (ds_User) and form variable and control values fillUsersDataSet(); fillCurrentUserVariables(UserNumber); setUserDetails(UserNumber); setUserNotes(UserNumber); setUserProjects(UserNumber); success = true; } } return(success); }