/// <summary> /// Registers a new company assuming all the fields are entered correctly. /// </summary> protected void CreateUser_Click(object sender, EventArgs e) { try { string userName, surname, forename, title, licenseNo, companyName, phoneNo, mobileNo, emailAddress; bool insertCustomer = true; //boolean to check all fields are entered correctly long companyID; DateTime issueDate, expirationDate, dateOfBirth; #region customerCheck if (companyDdl.SelectedValue != "") { companyID = Convert.ToInt32(companyDdl.SelectedValue.Split(',')[0]); companyName = companyDdl.SelectedValue.Split(',')[1]; } else { companyID = 0; companyName = ""; } if (userNameTxt.Text != "") { userName = userNameTxt.Text; } else { userName = ""; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a user name."; } if (Variables.CheckAlphabetCharacters(surnameTxt.Text) && surnameTxt.Text != "") { surname = surnameTxt.Text; } else { surname = ""; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a surname with only letters."; } if (Variables.CheckAlphabetCharacters(forenameTxt.Text) && forenameTxt.Text != "") { forename = forenameTxt.Text; } else { forename = ""; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a forename with only letters."; } if (titleDdl.SelectedValue != "Title") { title = titleDdl.SelectedValue; } else { title = ""; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a title."; } if (Variables.CheckAlphaNumericCharacters(licenseNoTxt.Text) && licenseNoTxt.Text != "") { licenseNo = licenseNoTxt.Text; } else { licenseNo = ""; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a valid driving license number."; } if (issueDaysDdl.SelectedValue != "" && issueMonthsDdl.SelectedValue != "" && issueYearsDdl.SelectedValue != "") { issueDate = Convert.ToDateTime(issueDaysDdl.SelectedValue + "/" + issueMonthsDdl.SelectedValue + "/" + issueYearsDdl.SelectedValue); } else { issueDate = DateTime.Now; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter an issue date for your license."; } if (expirationDaysDdl.SelectedValue != "" && expirationMonthsDdl.SelectedValue != "" && expirationYearsDdl.SelectedValue != "") { expirationDate = Convert.ToDateTime(expirationDaysDdl.SelectedValue + "/" + expirationMonthsDdl.SelectedValue + "/" + expirationYearsDdl.SelectedValue); } else { expirationDate = DateTime.Now; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter an expiration date for your license."; } if (dateOfBirthDaysDdl.SelectedValue != "" && dateOfBirthMonthsDdl.SelectedValue != "" && dateOfBirthYearsDdl.SelectedValue != "") { dateOfBirth = Convert.ToDateTime(dateOfBirthDaysDdl.SelectedValue + "/" + dateOfBirthMonthsDdl.SelectedValue + "/" + dateOfBirthYearsDdl.SelectedValue); } else { dateOfBirth = DateTime.Now; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter an date of birth"; } phoneNo = Request["phoneNoTxt"]; if (phoneNo == "") { insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a phone no."; } mobileNo = Request["mobileNoTxt"]; if (emailAddressTxt.Text != "") { emailAddress = emailAddressTxt.Text; } else { emailAddress = ""; insertCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a email address."; } if (Variables.CheckPasswordValid(passwordTxt.Text) != true) { insertCustomer = false; ErrorMessage.Text = "Passwords must contain at least 1 upper case letter, 1 lower case letter" + ", 1 number or special character and be at least 6 characters in length"; } #endregion if (insertCustomer == true) { string passwordEncrypt; List <CustomerManager> customers = CustomerManager.GetCustomers(); passwordEncrypt = PasswordHash.CreateHash(passwordTxt.Text); if (customers.Where(x => x.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase)).ToList().Count <= 0) { CustomerManager.AddNewCustomer(companyID, userName, surname, forename, title, licenseNo, issueDate, expirationDate, dateOfBirth, phoneNo, mobileNo, emailAddress, passwordEncrypt); customerSavedLbl.Text = "Save successful"; CustomerManager customer = CustomerManager.GetCustomers().Where(x => x.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); Session["LoggedInType"] = "Customer"; Session["UserName"] = userName; Session["UserID"] = customer.CustomerID; //Return to the home page Response.Redirect("~/", false); } else { inputErrorLbl.Text = "An account with that username already exists. Please enter a different one."; } } } catch (Exception ex) { generalErrorLbl.Text = "An error has occured saying: " + ex.Message + " Please contact your system administrator."; } }
/// <summary> /// Update the customer in the database assuming all fields entered are correct. /// </summary> protected void UpdateUser_Click(object sender, EventArgs e) { try { string surname, forename, title, licenseNo, companyName, phoneNo, mobileNo, emailAddress; bool updateCustomer = true; //boolean to check all fields are entered correctly long companyID; DateTime issueDate, expirationDate, dateOfBirth; #region customerCheck if (companyDdl.SelectedValue != "") { companyID = Convert.ToInt32(companyDdl.SelectedValue.Split(',')[0]); companyName = companyDdl.SelectedValue.Split(',')[1]; } else { companyID = 0; companyName = ""; } if (Variables.CheckAlphabetCharacters(surnameTxt.Text) && surnameTxt.Text != "") { surname = surnameTxt.Text; } else { surname = ""; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a surname with only letters."; } if (Variables.CheckAlphabetCharacters(forenameTxt.Text) && forenameTxt.Text != "") { forename = forenameTxt.Text; } else { forename = ""; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a forename with only letters."; } if (titleDdl.SelectedValue != "Title") { title = titleDdl.SelectedValue; } else { title = ""; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a title."; } if (Variables.CheckAlphaNumericCharacters(licenseNoTxt.Text) && licenseNoTxt.Text != "") { licenseNo = licenseNoTxt.Text; } else { licenseNo = ""; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a valid driving license number."; } if (issueDaysDdl.SelectedValue != "" && issueMonthsDdl.SelectedValue != "" && issueYearsDdl.SelectedValue != "") { issueDate = Convert.ToDateTime(issueDaysDdl.SelectedValue + "/" + issueMonthsDdl.SelectedValue + "/" + issueYearsDdl.SelectedValue); } else { issueDate = DateTime.Now; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter an issue date for your license."; } if (expirationDaysDdl.SelectedValue != "" && expirationMonthsDdl.SelectedValue != "" && expirationYearsDdl.SelectedValue != "") { expirationDate = Convert.ToDateTime(expirationDaysDdl.SelectedValue + "/" + expirationMonthsDdl.SelectedValue + "/" + expirationYearsDdl.SelectedValue); } else { expirationDate = DateTime.Now; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter an expiration date for your license."; } if (dateOfBirthDaysDdl.SelectedValue != "" && dateOfBirthMonthsDdl.SelectedValue != "" && dateOfBirthYearsDdl.SelectedValue != "") { dateOfBirth = Convert.ToDateTime(dateOfBirthDaysDdl.SelectedValue + "/" + dateOfBirthMonthsDdl.SelectedValue + "/" + dateOfBirthYearsDdl.SelectedValue); } else { dateOfBirth = DateTime.Now; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter an date of birth"; } phoneNo = Request["phoneNoTxt"]; if (phoneNo == "") { updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a phone no."; } mobileNo = Request["mobileNoTxt"]; if (emailAddressTxt.Text != "") { emailAddress = emailAddressTxt.Text; } else { emailAddress = ""; updateCustomer = false; inputErrorLbl.Text = inputErrorLbl.Text + "<br />" + "Please enter a email address."; } #endregion if (updateCustomer == true) { CustomerManager.UpdateCustomer(Variables.GetUser(Session["UserID"].ToString()), companyID, surname, forename, title, licenseNo, issueDate, expirationDate, dateOfBirth, phoneNo, mobileNo, emailAddress); customerSavedLbl.Text = "Save successful"; } //Reload because 3rd party phone controller clears itself on postback LoadCustomerInfo(); } catch (Exception ex) { generalErrorLbl.Text = "An error has occured saying: " + ex.Message + " Please contact your system administrator."; } }