protected void insert_TBL_NGUOI_DUNG(DataTable dt) { foreach (DataRow row in dt.Rows) { //Add User TBL_NGUOI_DUNG objUserLogin = new TBL_NGUOI_DUNG(); objUserLogin.UserName = row["USER_NAME"].ToString().Trim(); objUserLogin.FullName = row["HO_VA_TEN"].ToString().Trim(); objUserLogin.Email = row["EMAIL"].ToString().Trim(); objUserLogin.Tel = row["DIEN_THOAI"].ToString().Trim(); objUserLogin.Password = Formats.GetMD5(strDefaultPass); objUserLogin.isEnabled = true; entities.TBL_NGUOI_DUNG.Add(objUserLogin); entities.SaveChanges(); //Assign to Department TBL_NGUOI_DUNG_PHONG_BAN objDept = new TBL_NGUOI_DUNG_PHONG_BAN(); objDept.UserID = objUserLogin.Id; objDept.DepartmentID = 1;//1: Phong PTSPDVPM objDept.isDeleted = false; entities.TBL_NGUOI_DUNG_PHONG_BAN.Add(objDept); entities.SaveChanges(); } }
protected void btnSubmit_Click(object sender, EventArgs e) { try { TBL_NGUOI_DUNG objSearchUser = new TBL_NGUOI_DUNG(); bool blPassword = true; if (String.IsNullOrEmpty(txtUsername.Text)) { Commons.ValidationFuncs.errorMessage_TimeDelay("You must type User name", Page); return; } if (String.IsNullOrEmpty(txtFullName.Text)) { Commons.ValidationFuncs.errorMessage_TimeDelay("You must type Full name", Page); return; } if (String.IsNullOrEmpty(txtUserID.Text)) //Create New { entities.TBL_NGUOI_DUNG.Add(objSearchUser); //Check Password if (String.IsNullOrEmpty(txtPassword_1.Text) || String.IsNullOrEmpty(txtPassword_2.Text)) { Commons.ValidationFuncs.errorMessage_TimeDelay("You must type the password", Page); return; } } else //Update { int intUserID = Convert.ToInt32(txtUserID.Text); objSearchUser = entities.TBL_NGUOI_DUNG.Where(x => x.Id == intUserID).FirstOrDefault(); } //check Password if (!String.IsNullOrEmpty(txtPassword_1.Text)) { if (!txtPassword_1.Text.Equals(txtPassword_2.Text)) { blPassword = false; } } else if (!String.IsNullOrEmpty(txtPassword_2.Text)) { if (!txtPassword_2.Text.Equals(txtPassword_1.Text)) { blPassword = false; } } if (!blPassword) { Commons.ValidationFuncs.errorMessage_TimeDelay("Password is incorrect", Page); return; } setInputs(objSearchUser); entities.SaveChanges(); //Save into JOS_GROUPS_USERS tbl var lstGrvObj = grvObj.GetSelectedFieldValues(grvObj.KeyFieldName); if ((lstGrvObj != null) && (lstGrvObj.Count > 0)) //Disable { //delete all records in JOS_GROUPS_USERS tbl with User's ID var lstDelete = entities.TBL_NGUOI_DUNG_PHONG_BAN.Where(x => x.UserID == objSearchUser.Id).ToList(); foreach (var itemDel in lstDelete) { entities.TBL_NGUOI_DUNG_PHONG_BAN.Remove(itemDel); entities.SaveChanges(); } //Insert into JOS_GROUPS_USERS foreach (int item in lstGrvObj) { TBL_NGUOI_DUNG_PHONG_BAN objGroupUser = new TBL_NGUOI_DUNG_PHONG_BAN(); objGroupUser.DepartmentID = item; objGroupUser.UserID = objSearchUser.Id; entities.TBL_NGUOI_DUNG_PHONG_BAN.Add(objGroupUser); entities.SaveChanges(); } } backURL(); } catch (Exception ex) { lbMessage.Text = ex.Message; } }