protected void CreatePatientButton_Click(object sender, EventArgs e) { if (!ddlDOBValidateAllSet.IsValid) { return; } int person_id = -1; int patient_id = -1; int register_patient_id = -1; bool patient_added = false; int mainDbUserID = -1; int phone_id = -1; int email_id = -1; bool contacts_added = false; try { string[] clinicInfo = ddlClinic.SelectedValue.Split(new string[] { "__" }, StringSplitOptions.None); string dbID = clinicInfo[0]; int siteID = Convert.ToInt32(clinicInfo[1]); int orgID = Convert.ToInt32(clinicInfo[2]); Session["DB"] = dbID; Session["SystemVariables"] = SystemVariableDB.GetAll(); txtEmailAddr.Text = txtEmailAddr.Text.Trim(); txtPhoneNumber.Text = txtPhoneNumber.Text.Trim(); if (!Utilities.IsValidEmailAddress(txtEmailAddr.Text)) { throw new CustomMessageException("Email must be in valid email format."); } txtLogin.Text = txtLogin.Text.Trim(); txtPwd.Text = txtPwd.Text.Trim(); txtFirstname.Text = txtFirstname.Text.Trim(); txtSurname.Text = txtSurname.Text.Trim(); // check if patient exists in the system, if so use existing patietn bool patientAlreadyExists = false; // check if email exists in the system if (!patientAlreadyExists) { if (ExistsAndCreatedLogin_FromEmail(orgID, txtPhoneNumber.Text, txtEmailAddr.Text, siteID, ref register_patient_id, ref phone_id, ref email_id)) { patientAlreadyExists = true; patient_added = true; contacts_added = true; this.lblErrorMessage.Text = "Your email alrady exist in this sytem.<br/>An email has been sent with new login details.<br/>When you receieve it, use the login link below."; } } // check if firstname / surname / DOB exists in the system if (!patientAlreadyExists) { if (ExistsAndCreatedLogin_FromNameAndDOB(orgID, txtPhoneNumber.Text, txtEmailAddr.Text, txtFirstname.Text, txtSurname.Text, GetDOBFromForm(), siteID, ref register_patient_id, ref phone_id, ref email_id)) { patientAlreadyExists = true; patient_added = true; contacts_added = true; this.lblErrorMessage.Text = "You alrady exist in this sytem.<br/>An email has been sent with new login details.<br/>When you receieve it, use the login link below."; } } if (!patientAlreadyExists) { if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]) && UserDatabaseMapperDB.UsernameExists(txtLogin.Text)) { throw new CustomMessageException("Login name already in use. Please choose another"); } if (PatientDB.LoginExists(txtLogin.Text)) { throw new CustomMessageException("Login name already in use. Please choose another"); } // 1. Create Patient Staff loggedInStaff = StaffDB.GetByID(-6); person_id = PersonDB.Insert(loggedInStaff.Person.PersonID, Convert.ToInt32(ddlTitle.SelectedValue), Utilities.FormatName(txtFirstname.Text), "", Utilities.FormatName(txtSurname.Text), "", ddlGender.SelectedValue, GetDOBFromForm()); patient_id = PatientDB.Insert(person_id, true, false, false, "", -1, DateTime.MinValue, "", "", DateTime.MinValue, false, false, DateTime.MinValue, -1, -1, txtLogin.Text, txtPwd.Text, false, "", "", "", ""); register_patient_id = RegisterPatientDB.Insert(orgID, patient_id); patient_added = true; // added this because was throwing a thread aborted exception after patient added before Response.Redirect if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"])) { if (txtLogin.Text.Length > 0) { mainDbUserID = UserDatabaseMapperDB.Insert(txtLogin.Text, Session["DB"].ToString()); } } // 2. Add Contact Info Patient patient = PatientDB.GetByID(patient_id); phone_id = AddPhoneNbrIfNotExists(patient, siteID, txtPhoneNumber.Text); email_id = AddEmailIfNotExists(patient, siteID, txtEmailAddr.Text); register_patient_id = AddOrgIfNotExists(patient, siteID, orgID); contacts_added = true; SendInfoEmail(txtEmailAddr.Text, txtLogin.Text, txtPwd.Text); this.lblErrorMessage.Text = "An email has been sent with new login details.<br />When you receieve it, use the login link below."; } } catch (Exception ex) { if (!patient_added || !contacts_added) { // roll back - backwards of creation order if (Utilities.GetAddressType().ToString() == "Contact") { ContactDB.Delete(phone_id); ContactDB.Delete(email_id); } else if (Utilities.GetAddressType().ToString() == "ContactAus") { ContactAusDB.Delete(phone_id); ContactAusDB.Delete(email_id); } else { throw new Exception("Unknown AddressType in config: " + Utilities.GetAddressType().ToString().ToString()); } RegisterPatientDB.Delete(register_patient_id); PatientDB.Delete(patient_id); PersonDB.Delete(person_id); if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"])) { UserDatabaseMapperDB.Delete(mainDbUserID); } if (ex is CustomMessageException) { this.lblErrorMessage.Text = ex.Message; } else { lblErrorMessage.Text = ex.ToString(); } } } finally { //Session["DB"] = curDbName; //Session["SystemVariables"] = SystemVariableDB.GetAll(); Session.Remove("DB"); Session.Remove("SystemVariables"); } }