public UserDetailsAll GetUserDetails(int uid, out bool success) { success = false; UserDetailsAll ud = new UserDetailsAll(); SqlConnection con = null; try { con = new SqlConnection(conn); SqlCommand cmd = new SqlCommand("Select * from users where uid=@uid", con); cmd.Parameters.AddWithValue("@uid", uid); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { ud.Username = dr["username"].ToString(); ud.Name = dr["name"].ToString(); ud.Email = dr["email"].ToString(); ud.College = dr["college"].ToString(); ud.Branch = dr["branch"].ToString(); ud.Gender = dr["gender"].ToString(); ud.DOB = dr["dob"].ToString(); success = true; } } con.Close(); } catch (Exception e1) { //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Error:" + e1.Message + "')", true); } return(ud); }
public ActionResult EditProfile(UserDetailsAll user) { //Option 1 //TryUpdateModel<UserDetailsAll>(user, null, // new string[] { "Name", "Email", "DOB", "College", "Branch", "Gender", "Image" },//Include these in ModelState.IsValid // new string[] { "Username", "Password", "c_pwd" });//Exclude these from ModelState.IsValid //Option 2 ModelState.Remove("Username"); ModelState.Remove("Password"); ModelState.Remove("c_pwd"); ModelState.Remove("Image"); if (ModelState.IsValid) { //Call Register Method bool success = false; ViewBag.message = bl.UpdateUserDetails(user, Session["uid"].ToString(), out success); if (success == true) { return(View()); } } return(View()); }
public ActionResult Register(UserDetailsAll user) { if (ModelState.IsValid) { //Call Register Method bool success = false; ViewBag.message = bl.RegisterMethod(user, out success); if (success == true) { ModelState.Clear(); return(View()); } } return(View()); }
public ActionResult HomePage() { bool success = false; if (Session["uid"] != null) { int uid = Convert.ToInt32(Session["uid"].ToString()); UserDetailsAll ud = bl.GetUserDetails(uid, out success); MultipleModel m = new MultipleModel(); Group g = new Group(); m.UserDetails = ud; m.group = g; if (success == true) { bool success1 = false; string ImageUrl = bl.GetUserImage(uid, out success1); if (success1 == true) { ViewBag.ImageUrl = ImageUrl; } else { ViewBag.message = "Error occured while fetching image."; } if (TempData["message"] != null) { ViewBag.message = TempData["message"].ToString(); } return(View(m)); } else { ViewBag.message = "Error occured while fetching details. Please contact admin."; return(View()); } } else { return(RedirectToAction("Login")); } }
public ActionResult EditProfile() { bool success = false; if (Session["uid"] != null) { int uid = Convert.ToInt32(Session["uid"].ToString()); UserDetailsAll ud = bl.GetUserDetails(uid, out success); if (success == true) { return(View(ud)); } else { ViewBag.message = "Error occured while fetching details. Please contact admin."; return(View()); } } else { return(RedirectToAction("Login")); } }
public string RegisterMethod(UserDetailsAll user, out bool success) { success = false; SqlConnection con = null; try { con = new SqlConnection(conn); SqlCommand cmd = new SqlCommand("Select username from users where username=@user", con); cmd.Parameters.AddWithValue("@user", user.Username); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { con.Close(); return("Username exists..try different one"); //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Username exists..try different one')", true); } else { con.Close(); if (user.Image.ContentLength > 0) { SqlCommand cmd2 = new SqlCommand("insert into users values(@name,@email,@dob,@college,@branch,@gender,@username,@pass,@image,@status,@statustime,@online)", con); cmd2.Parameters.AddWithValue("@name", user.Name); cmd2.Parameters.AddWithValue("@email", user.Email); //cmd2.Parameters.AddWithValue("@dob", ddlDay.Text + "-" + ddlMonth.Text + "-" + ddlYear.Text); cmd2.Parameters.AddWithValue("@dob", user.DOB); cmd2.Parameters.AddWithValue("@college", user.College); cmd2.Parameters.AddWithValue("@branch", user.Branch); cmd2.Parameters.AddWithValue("@gender", user.Gender); cmd2.Parameters.AddWithValue("@username", user.Username); cmd2.Parameters.AddWithValue("@pass", user.Password); cmd2.Parameters.AddWithValue("@image", user.Image.FileName); cmd2.Parameters.AddWithValue("@status", ""); cmd2.Parameters.AddWithValue("@statustime", ""); cmd2.Parameters.AddWithValue("@online", false); con.Open(); cmd2.ExecuteNonQuery(); //string filename = Path.GetFileName(fuImage.FileName); user.Image.SaveAs(HttpContext.Current.Server.MapPath("~/images/user_images/") + user.Image.FileName); //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('You have successfully registered')", true); //txtCollege.Text = txtConfPass.Text = txtEmail.Text = txtName.Text = txtPass.Text = txtUsername.Text = ""; //ddlBranch.SelectedIndex = ddlDay.SelectedIndex = ddlGender.SelectedIndex = ddlMonth.SelectedIndex = ddlYear.SelectedIndex = 0; con.Close(); success = true; return("You have successfully registered!!!"); } else { con.Close(); return("Upload your image to register"); //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Upload your image to register')", true); } } } catch (Exception e1) { return("Some Error Occurred. Please contact Admin"); //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Error:" + e1.Message + "')", true); } }
public string UpdateUserDetails(UserDetailsAll ud, string uid, out bool success) { success = false; SqlConnection con = null; string existingImg = ""; #region Get existing image try { con = new SqlConnection(conn); SqlCommand cmd2 = new SqlCommand("Select uimage from users where uid=@uid", con); cmd2.Parameters.AddWithValue("@uid", uid); con.Open(); SqlDataReader dr = cmd2.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { existingImg = dr["uimage"].ToString(); } } con.Close(); } catch (Exception e2) { //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Error:" + e2.Message + "')", true); } #endregion try { con = new SqlConnection(conn); SqlCommand cmd = new SqlCommand("Update users set name=@name,email=@email,dob=@dob,college=@college,branch=@branch,gender=@gender,uimage=@uimage where uid=@uid", con); cmd.Parameters.AddWithValue("@uid", uid); cmd.Parameters.AddWithValue("@name", ud.Name); cmd.Parameters.AddWithValue("@email", ud.Email); cmd.Parameters.AddWithValue("@dob", ud.DOB); cmd.Parameters.AddWithValue("@college", ud.College); cmd.Parameters.AddWithValue("@branch", ud.Branch); cmd.Parameters.AddWithValue("@gender", ud.Gender); if (ud.Image != null) { cmd.Parameters.AddWithValue("@uimage", ud.Image.FileName); } else { cmd.Parameters.AddWithValue("@uimage", existingImg); } con.Open(); cmd.ExecuteNonQuery(); if (ud.Image != null) { ud.Image.SaveAs(HttpContext.Current.Server.MapPath("~/images/user_images/") + ud.Image.FileName); } con.Close(); success = true; return("Your profile has been updated."); //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Your profile has been updated.')", true); } catch (Exception e1) { return("Error occured."); //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Error:" + e1.Message + "')", true); } }