protected void GrdStaff_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("Insert")) { CustomValidator txtValidateDOB = (CustomValidator)GrdStaff.FooterRow.FindControl("txtValidateNewDOB"); if (!txtValidateDOB.IsValid) { return; } DropDownList ddlTitle = (DropDownList)GrdStaff.FooterRow.FindControl("ddlNewTitle"); TextBox txtFirstname = (TextBox)GrdStaff.FooterRow.FindControl("txtNewFirstname"); TextBox txtMiddlename = (TextBox)GrdStaff.FooterRow.FindControl("txtNewMiddlename"); TextBox txtSurname = (TextBox)GrdStaff.FooterRow.FindControl("txtNewSurname"); DropDownList ddlGender = (DropDownList)GrdStaff.FooterRow.FindControl("ddlNewGender"); TextBox txtDOB = (TextBox)GrdStaff.FooterRow.FindControl("txtNewDOB"); TextBox txtLogin = (TextBox)GrdStaff.FooterRow.FindControl("txtNewLogin"); TextBox txtPwd = (TextBox)GrdStaff.FooterRow.FindControl("txtNewPwd"); //DropDownList ddlStaffPosition = (DropDownList)GrdStaff.FooterRow.FindControl("ddlNewStaffPosition"); DropDownList ddlField = (DropDownList)GrdStaff.FooterRow.FindControl("ddlNewField"); CheckBox chkContractor = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewContractor"); TextBox txtTFN = (TextBox)GrdStaff.FooterRow.FindControl("txtNewTFN"); DropDownList ddlStatus = (DropDownList)GrdStaff.FooterRow.FindControl("ddlStatus"); DropDownList ddlCostCentre = (DropDownList)GrdStaff.FooterRow.FindControl("ddlNewCostCentre"); TextBox txtProviderNumber = (TextBox)GrdStaff.FooterRow.FindControl("txtNewProviderNumber"); CheckBox chkIsCommission = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewIsCommission"); TextBox txtCommissionPercent = (TextBox)GrdStaff.FooterRow.FindControl("txtNewCommissionPercent"); CheckBox chkIsStakeholder = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewIsStakeholder"); CheckBox chkIsAdmin = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewIsAdmin"); CheckBox chkIsMasterAdmin = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewIsMasterAdmin"); CheckBox chkIsPrincipal = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewIsPrincipal"); CheckBox chkIsProvider = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewIsProvider"); CheckBox chkSMSBKs = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewSMSBKs"); CheckBox chkEmailBKs = (CheckBox)GrdStaff.FooterRow.FindControl("chkNewEmailBKs"); if (chkIsProvider.Checked && (StaffDB.GetCountOfProviders() >= Convert.ToInt32(SystemVariableDB.GetByDescr("MaxNbrProviders").Value))) { SetErrorMessage("You have reached your maximum allowable providers. Please uncheck their status as a provider to add them. Contact Mediclinic if you would like to upgrade your account."); return; } if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]) && UserDatabaseMapperDB.UsernameExists(txtLogin.Text)) { SetErrorMessage("Login name already in use by another user"); return; } if (StaffDB.LoginExists(txtLogin.Text)) { SetErrorMessage("Login name already in use by another user"); return; } if (txtPwd.Text.Length < 6) { SetErrorMessage("Password must be at least 6 characters"); return; } DateTime dob = GetDate(txtDOB.Text.Trim()); int person_id = -1; int mainDbUserID = -1; try { if (!!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"])) { mainDbUserID = UserDatabaseMapperDB.Insert(txtLogin.Text, Session["DB"].ToString()); } if (chkIsMasterAdmin.Checked) { chkIsAdmin.Checked = true; } Staff loggedInStaff = StaffDB.GetByID(Convert.ToInt32(Session["StaffID"])); person_id = PersonDB.Insert(loggedInStaff.Person.PersonID, Convert.ToInt32(ddlTitle.SelectedValue), Utilities.FormatName(txtFirstname.Text), Utilities.FormatName(txtMiddlename.Text), Utilities.FormatName(txtSurname.Text), "", ddlGender.SelectedValue, dob); StaffDB.Insert(person_id, txtLogin.Text, txtPwd.Text, StaffPositionDB.GetByDescr("Unknown").StaffPositionID, Convert.ToInt32(ddlField.SelectedValue), Convert.ToInt32(ddlCostCentre.SelectedValue), chkContractor.Checked, txtTFN.Text, txtProviderNumber.Text.ToUpper(), ddlStatus.SelectedValue == "Inactive", chkIsCommission.Checked, Convert.ToDecimal(txtCommissionPercent.Text), chkIsStakeholder.Checked, chkIsMasterAdmin.Checked, chkIsAdmin.Checked, chkIsPrincipal.Checked, chkIsProvider.Checked, false, DateTime.Today, DateTime.MinValue, "", chkSMSBKs.Checked, chkEmailBKs.Checked); FillGrid(); } catch (Exception) { // roll back - backwards of creation order PersonDB.Delete(person_id); if (!!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"])) { UserDatabaseMapperDB.Delete(mainDbUserID); } } } }
protected void GrdStaff_RowUpdating(object sender, GridViewUpdateEventArgs e) { Label lblId = (Label)GrdStaff.Rows[e.RowIndex].FindControl("lblId"); DropDownList ddlTitle = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlTitle"); TextBox txtFirstname = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtFirstname"); TextBox txtMiddlename = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtMiddlename"); TextBox txtSurname = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtSurname"); DropDownList ddlGender = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlGender"); DropDownList ddlDOB_Day = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlDOB_Day"); DropDownList ddlDOB_Month = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlDOB_Month"); DropDownList ddlDOB_Year = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlDOB_Year"); TextBox txtLogin = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtLogin"); TextBox txtPwd = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtPwd"); //DropDownList ddlStaffPosition = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlStaffPosition"); DropDownList ddlField = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlField"); CheckBox chkContractor = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkContractor"); TextBox txtTFN = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtTFN"); DropDownList ddlStatus = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlStatus"); DropDownList ddlCostCentre = (DropDownList)GrdStaff.Rows[e.RowIndex].FindControl("ddlCostCentre"); TextBox txtProviderNumber = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtProviderNumber"); CheckBox chkIsCommission = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkIsCommission"); TextBox txtCommissionPercent = (TextBox)GrdStaff.Rows[e.RowIndex].FindControl("txtCommissionPercent"); CheckBox chkIsStakeholder = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkIsStakeholder"); CheckBox chkIsAdmin = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkIsAdmin"); CheckBox chkIsMasterAdmin = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkIsMasterAdmin"); CheckBox chkIsPrincipal = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkIsPrincipal"); CheckBox chkIsProvider = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkIsProvider"); CheckBox chkSMSBKs = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkSMSBKs"); CheckBox chkEmailBKs = (CheckBox)GrdStaff.Rows[e.RowIndex].FindControl("chkEmailBKs"); int staff_id = Convert.ToInt32(lblId.Text); int person_id = GetPersonID(Convert.ToInt32(lblId.Text)); if (person_id == -1) // happens when back button hit after update .. with option to update again ... but no selected row exists within page data { GrdStaff.EditIndex = -1; FillGrid(); return; } Staff staff = StaffDB.GetByID(staff_id); if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]) && staff.Login != txtLogin.Text && UserDatabaseMapperDB.UsernameExists(txtLogin.Text)) { SetErrorMessage("Login name already in use by another user"); return; } if (staff.Login != txtLogin.Text && StaffDB.LoginExists(txtLogin.Text, staff_id)) { SetErrorMessage("Login name already in use by another user"); return; } if (staff.Pwd != txtPwd.Text && txtPwd.Text.Length < 6) { SetErrorMessage(staff.Pwd.Length >= 6 ? "Password must be at least 6 characters" : "New passwords must be at least 6 characters"); return; } DataTable dt = Session["staffinfo_data"] as DataTable; DataRow[] foundRows = dt.Select("person_id=" + person_id.ToString()); DataRow row = foundRows[0]; // Convert.ToInt32(row["person_id"]) if (!Convert.ToBoolean(row["is_provider"]) && chkIsProvider.Checked && (StaffDB.GetCountOfProviders() >= Convert.ToInt32(SystemVariableDB.GetByDescr("MaxNbrProviders").Value))) { SetErrorMessage("You have reached your maximum allowable providers. Please uncheck their status as a provider to update them or hit cancel. Contact Mediclinic if you would like to upgrade your account."); return; } if (chkIsProvider.Checked) { System.Data.DataTable tbl = DBBase.GetGenericDataTable_WithWhereOrderClause(null, "Field", "has_offerings=1 AND field_id <> 0", "", "field_id", "descr"); bool roleSetAsProvider = false; IDandDescr[] fields = new IDandDescr[tbl.Rows.Count]; for (int i = 0; i < tbl.Rows.Count; i++) { fields[i] = new IDandDescr(Convert.ToInt32(tbl.Rows[i]["field_id"]), tbl.Rows[i]["descr"].ToString()); if (Convert.ToInt32(ddlField.SelectedValue) == Convert.ToInt32(tbl.Rows[i]["field_id"])) { roleSetAsProvider = true; } } if (!roleSetAsProvider) { if (fields.Length == 1) { SetErrorMessage("When setting a staff member as a provider, you need to set their Role as '" + fields[0].Descr + "'."); return; } else if (fields.Length == 2) { SetErrorMessage("When setting a staff member as a provider, you need to set their Role as '" + fields[0].Descr + "' or '" + fields[1].Descr + "'."); return; } else { string providerFields = string.Empty; for (int i = 0; i < fields.Length; i++) { providerFields += (providerFields.Length == 0 ? "" : ", ") + (fields.Length >= 2 && i == (fields.Length - 2) ? "or " : "") + fields[i].Descr; } SetErrorMessage("When setting a staff member as a provider, you need to set their Role as one of the following: " + providerFields); return; } } } if (chkIsMasterAdmin.Checked) { chkIsAdmin.Checked = true; } PersonDB.Update(person_id, Convert.ToInt32(ddlTitle.SelectedValue), Utilities.FormatName(txtFirstname.Text), Utilities.FormatName(txtMiddlename.Text), Utilities.FormatName(txtSurname.Text), row["nickname"].ToString(), ddlGender.SelectedValue, GetDate(ddlDOB_Day.SelectedValue, ddlDOB_Month.SelectedValue, ddlDOB_Year.SelectedValue), DateTime.Now); StaffDB.Update(staff_id, person_id, txtLogin.Text, txtPwd.Text, Convert.ToInt32(row["staff_position_id"]), Convert.ToInt32(ddlField.SelectedValue), Convert.ToInt32(ddlCostCentre.SelectedValue), chkContractor.Checked, txtTFN.Text, txtProviderNumber.Text.ToUpper(), ddlStatus.SelectedValue == "Inactive", chkIsCommission.Checked, Convert.ToDecimal(txtCommissionPercent.Text), chkIsStakeholder.Checked, chkIsMasterAdmin.Checked, chkIsAdmin.Checked, chkIsPrincipal.Checked, chkIsProvider.Checked, staff.IsExternal, row["start_date"] == DBNull.Value ? DateTime.MinValue : (DateTime)row["start_date"], row["end_date"] == DBNull.Value ? DateTime.MinValue : (DateTime)row["end_date"], row["comment"].ToString(), chkSMSBKs.Checked, chkEmailBKs.Checked); if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]) && staff.Login != txtLogin.Text) { UserDatabaseMapper curDBMapper = UserDatabaseMapperDB.GetByLogin(staff.Login, Session["DB"].ToString()); if (curDBMapper == null) { UserDatabaseMapperDB.Insert(txtLogin.Text, Session["DB"].ToString()); } else { UserDatabaseMapperDB.Update(curDBMapper.ID, txtLogin.Text, Session["DB"].ToString()); } } GrdStaff.EditIndex = -1; FillGrid(); }