protected void btnSave_Click(object sender, EventArgs e) { var user = Page.User as CustomPrincipal; if (user != null) { using (var fileHelper = new DbHelper.WorkingWithFiles()) using (var helper = new DbHelper.User()) { if (helper.DoesUserNameExist(user.SchoolId,UserId, txtUserName.Text)) { valiUserName.ErrorMessage = "Username already exists"; valiUserName.IsValid = false; } if (Page.IsValid) { var dob = DateTime.MinValue; try { dob = Convert.ToDateTime(txtDOB.Text); } catch { } var date = DateTime.Now.Date; var createdUser = new Academic.DbEntities.User.Users() { SchoolId = user.SchoolId, City = txtCity.Text, Country = txtCountry.Text, CreatedDate = date, Email = txtEmail.Text, FirstName = txtFirstName.Text, MiddleName = txtMidName.Text, LastName = txtLastName.Text, IsActive = true, IsDeleted = false, UserName = txtUserName.Text, Password = txtPassword.Text, Id = UserId, Phone = txtPhone1.Text, //Description = txtDescription.Text, }; if (cmbGender.SelectedValue!=""&& cmbGender.SelectedValue!="0") { createdUser.GenderId = Convert.ToInt32(cmbGender.SelectedValue); } if (dob != DateTime.MinValue) createdUser.DOB = dob; var userPhotoDirectory = fileHelper.GetUserPhotoFolder(user.SchoolId); if (userPhotoDirectory != null) { var files = FilesDisplay.GetFiles(); UserFile image = null; if (files != null) { if (files.Count >= 1) { var f = files[0]; //foreach (var f in files) { var fileName = Path.GetFileName(f.FilePath); image = new Academic.DbEntities.UserFile() { Id = f.Id, CreatedBy = user.Id, CreatedDate = DateTime.Now, DisplayName = f.FileDisplayName, //Path.GetFileName(imageFile.FileName) FileDirectory = DbHelper.StaticValues.UserImageDirectory, //StaticValue.UserImageDirectory FileName = fileName, //Guid.NewGuid().ToString() + GetExtension(imageFile.FileName, imageFile.ContentType) FileSizeInBytes = f.FileSizeInBytes, //imageFile.ContentLength FileType = f.FileType, //imageFile.ContentType IconPath = f.IconPath, SchoolId = user.SchoolId, IsServerFile = true, IsConstantAndNotEditable = false, FolderId = userPhotoDirectory.Id, //SubjectId = SubjectId, }; if (f.Id > 0) { image.ModifiedBy = user.Id; image.ModifiedDate = DateTime.Now; } } } } //var userRole = new Academic.DbEntities.User.UserRole() //{ // RoleId = Convert.ToInt32(ddlRole.SelectedValue), // UserId = UserId //}; var savedUser = helper.AddOrUpdateUser(createdUser, ddlRole.SelectedValue, image); if (savedUser != null) { if (UserId > 0) { Response.Redirect("~/Views/User/Detail.aspx?uId=" + UserId); } else { Response.Redirect("List.aspx"); } } } else { //show error of "Folder unable to find" lblSaveStatus.Text = "'User Photos' directory not found."; lblSaveStatus.Visible = true; } } } } }
//private void PopulateCountry() //{ // List<string> countries = new List<string>(); // var getCultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures); // foreach (var c in getCultureInfo) // { // var getRegionInfo = // new RegionInfo(c.LCID); // var name = getRegionInfo.EnglishName.Split(new char[] { '(' })[0]; // if (!countries.Contains(name)) // { // countries.Add(name); // } // } // countries.Sort(); // var i = 0; // try // { // i = countries.IndexOf("Nepal"); // } // catch // { // } // if (i != 0) // countries.Insert(0, "Select"); // ddlCountry.DataSource = countries; // ddlCountry.DataBind(); // ddlCountry.SelectedIndex = i; //} protected void btnSave_Click(object sender, EventArgs e) { //if (cmbSchoolType.SelectedValue == "" || cmbSchoolType.SelectedValue == "0") //{ // valiSchType.IsValid = false; //} //if (ddlCountry.SelectedIndex == 0) // valiCountry.IsValid = false; if (IsValid) { var user = User as CustomPrincipal; if (user == null) { return; } var school = new Academic.DbEntities.Office.School() { Id = user.SchoolId, Name = txtName.Text, Address = txtAddress.Text, EmailGeneral = txtEmailGeneral.Text, EmailMarketing = "", //txtEmailMarketing.Text, EmailSupport = "", //txtEmailSupport.Text, IsActive = chkActive.Checked, PhoneMain = txtPhoneMain.Text, PhoneAfterHours = "", //txtPhoneAfterHours.Text, Description = CKEditor1.Text, //SchoolTypeId = Convert.ToInt32(cmbSchoolType.Text), Website = txtWeb.Text, CreatedDate = DateTime.Now, ImageId = Convert.ToInt32(hidImageId.Value) }; if (user.SchoolId <= 0) { school.UserId = user.Id; } using (var helper = new DbHelper.Office()) { var files = FilesDisplay1.GetFiles(); var image = new UserFile(); if (files != null) { if (files.Count >= 1) { var f = files[0]; //foreach (var f in files) { var fileName = Path.GetFileName(f.FilePath); image = new Academic.DbEntities.UserFile() { Id = f.Id, CreatedBy = user.Id , CreatedDate = DateTime.Now , DisplayName = f.FileDisplayName //Path.GetFileName(imageFile.FileName) , FileDirectory = DbHelper.StaticValues.SchoolFileLocation //StaticValue.UserImageDirectory , FileName = fileName //Guid.NewGuid().ToString() + GetExtension(imageFile.FileName, imageFile.ContentType) , FileSizeInBytes = f.FileSizeInBytes //imageFile.ContentLength , FileType = f.FileType //imageFile.ContentType , IconPath = f.IconPath , //SubjectId = SubjectId }; if (f.Id > 0) { image.ModifiedBy = user.Id; image.ModifiedDate = DateTime.Now; } } } } var saved = helper.AddOrUpdateSchool(school, image);//FileUpload1.PostedFile //update cookie -- add school Id to the cookie //Page.User.Identity.Name; if (saved != null) { if (user.SchoolId <= 0) { var ok = UpdateSchoolInfoInCookie(user, saved.Id); if (ok) { Response.Redirect("~/ViewsSite/User/Dashboard/Dashboard.aspx"); } else { lblMsg.Text = "Error while saving."; } //---not needed, since redirect is done above //SchoolTypeUC.SavedId = 0; //lblMsg.Visible = true; //lblMsg.Text = "Save Successful."; } else { Response.Redirect("~/About.aspx"); //"//~/Views/Office/"); } } else { lblMsg.Text = "Error while saving."; } } } else { lblMsg.Text = "Some fields are invalid or not filled."; } }