private int CheckStaffNameExist(string displayName, int staffNameId, int staffId) { RunStoredProcedure rsp = new RunStoredProcedure(); // check if staff name exists in db int exist = rsp.StoredProcedureReturnInt("Proc_CheckStaffNameExist", "staffName", displayName, "count"); // if staff name exists, activate the correct staff name from db if (exist > 0) { // activate staff name on logged in user rsp.StoredProcedureUpdateBool("Proc_UpdateStaffName_Varchar", "active", true, "staffName", displayName); // get staff name id staffNameId = rsp.StoredProcedureReturnInt("Proc_GetStaffNameId_StaffName", "staffName", displayName, "staffNameId"); } // staff name does not exists in db, create a new staff name id and make it active else { // add a new staff name and return staff name id staffNameId = rsp.StoredProcedureInsertRow("Proc_AddStaffName", "staffName", displayName, "staffNameId"); } // update staff name id in staff table rsp.StoredProcedureUpdateInt("Proc_UpdateStaffNameId", "staffNameId", staffNameId, "staffId", staffId); return(staffNameId); }
// upon login, check staff details and update necessary details private void Staff(string displayName, string group) { // check whether the staff logged in has the same credentials compared from the previous credentials logged in - if not, update db RunStoredProcedure rsp = new RunStoredProcedure(); var staffId = 0; var staffNameId = 0; try { staffId = rsp.StoredProcedureReturnInt("Proc_GetStaffId", "username", txtUsername.Text, "staffId"); } catch { // set staffId to zero to add the staff details to db staffId = 0; } // if staff exist in the db - next step is to compare from previous login and update staff details if (staffId != 0) { staffNameId = rsp.StoredProcedureReturnInt("Proc_GetStaffNameId_Staff", "username", txtUsername.Text, "staffNameId"); string staffName = rsp.StoredProcedureReturnString("Proc_GetStaffName", "staffId", staffId, "staffName"); // staff name is different from ad compared to db if (!displayName.Equals(staffName)) { // disable active staff name on logged in user rsp.StoredProcedureUpdateBool("Proc_UpdateStaffName_Int", "active", false, "staffNameId", staffNameId); // add or update staff name from db staffNameId = CheckStaffNameExist(displayName, staffNameId, staffId); } } // staff does not exist in db - create a new staff id else { // add or update staff name from db staffNameId = CheckStaffNameExist(displayName, staffNameId, staffId); // add a new staff name and return staff name id staffId = rsp.StoredProcedureInsertRow("Proc_AddStaff", "staffNameId", staffNameId, "username", txtUsername.Text, "staffId"); } UserCredentials.StaffId = staffId; UserCredentials.StaffNameId = staffNameId; // upon login, update user's role and group access UpdateStaffDetails(group, staffId); }