protected void grdUser_RowCommand(object sender, GridViewCommandEventArgs e)
 {
     try
     {
         if (e.CommandName.Equals("Delete"))
         {
             User user = new User();
             int index = Convert.ToInt32(e.CommandArgument);
             GridViewRow row = grdUser.Rows[index];
             HiddenField hdnField = (HiddenField)row.FindControl("hdnGUID");
             user.deleteUserByUserId(new Guid(hdnField.Value.ToString()));
             bindUsers();
         }
         if (e.CommandName.Equals("Edit"))
         {
             int index = Convert.ToInt32(e.CommandArgument);
             GridViewRow row = grdUser.Rows[index];
             HiddenField hdnField = (HiddenField)row.FindControl("hdnGUID");
             Response.Redirect("ManageUser.aspx?guid=" + new Guid(hdnField.Value.ToString()));
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 private void bindUsers()
 {
     User user = new User();
     try
     {
         grdUser.DataSource = user.getAllUsers();
         grdUser.DataBind();
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         user = null;
     }
 }
 protected void btnSave_Click(object sender, EventArgs e)
 {
     User objUser = new User();
     try
     {
         if (txtOldPassword.Text == "")
         {
             lblError.Visible = true;
             lblError.InnerHtml = "Please enter old password";
         }
         else if (txtNewPassword.Text == "")
         {
             lblError.Visible = true;
             lblError.InnerHtml = "Please enter new password";
         }
         else if (txtConfirmPassword.Text != txtNewPassword.Text)
         {
             lblError.Visible = true;
             lblError.InnerHtml = "New password and confirm password should be same";
         }
         else
         {
             objUser.GUID = new Guid(HttpContext.Current.Session["User"].ToString());
             objUser.Password = PasswordHash.CreateHash(txtNewPassword.Text);
             objUser.updatePassword(txtNewPassword.Text);
             lblError.Visible = true;
             lblError.InnerHtml = "Review added successfully";
         }
     }
     catch (Exception ex)
     {
         lblError.Visible = true;
         lblError.InnerHtml = ex.Message;
     }
     finally
     {
         objUser = null;
     }
 }
        protected void lbtnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Trim() == string.Empty)
            {
                lblError.Visible = true;
                lblError.InnerHtml = "Please enter User Name";
            }
            else if (txtPassword.Text.Trim() == string.Empty)
            {
                lblError.Visible = true;
                lblError.InnerHtml = "Please enter Password";
            }
            else
            {
                Models.User objUser = new Models.User();
                DataTable dt = objUser.getUsersForLogin(txtUserName.Text.Trim());
                if (dt.Rows.Count > 0)
                {
                    if (PasswordHash.ValidatePassword(txtPassword.Text.Trim(), dt.Rows[0]["Password"].ToString()))
                    {
                        SystemSession.UserID = new Guid(dt.Rows[0]["GUID"].ToString());
                        FormsAuthentication.SetAuthCookie(dt.Rows[0]["UserName"].ToString(), false);
                        FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
                        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                        if (authCookie != null)
                        {
                            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

                            var newUser = new CustomPrincipal(authTicket.Name)
                            {
                                UserId = new Guid(dt.Rows[0]["GUID"].ToString()),
                                UserName = dt.Rows[0]["UserName"].ToString(),
                                FirstName = dt.Rows[0]["FirstName"].ToString(),
                                LastName = dt.Rows[0]["LastName"].ToString()
                            };
                            HttpContext.Current.User = newUser;
                        }
                        if (HttpContext.Current.User.Identity.IsAuthenticated)
                        {
                            HttpContext.Current.Session["User"] = dt.Rows[0]["GUID"].ToString();
                            if (Request.QueryString["ReturnUrl"] == null)
                            {
                                Response.Redirect("~/UserModule/ShowAllUsers.aspx");
                            }
                            else
                            {
                                Response.Redirect(Request.QueryString["ReturnUrl"].ToString());
                            }
                        }
                    }
                    else
                    {
                        lblError.Visible = true;
                        lblError.InnerHtml = "Invalid UserName/Password. Please contact Administrator";
                    }
                }
                else
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Invalid UserName/Password. Please contact Administrator";
                }

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            User objAddNewUser = new User();
            try
            {
                if (string.IsNullOrWhiteSpace(txtUserName.Text))
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Please enter User Name";
                }
                else if (string.IsNullOrWhiteSpace(txtUserEmail.Text))
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Please enter User Email";
                }
                else if (string.IsNullOrWhiteSpace(txtUserPhone.Text))
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Please enter User Phone";
                }
                else if (string.IsNullOrWhiteSpace(txtUserLocation.Text))
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Please enter User Location";
                }
                else if (string.IsNullOrWhiteSpace(txtFirstName.Text))
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Please enter First Name";
                }
                else if (string.IsNullOrWhiteSpace(txtLastName.Text))
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Please enter Last Name";
                }
                else if (string.IsNullOrWhiteSpace(txtPassword.Text))
                {
                    lblError.Visible = true;
                    lblError.InnerHtml = "Please enter Password";
                }
                else
                {
                    if (Request.QueryString["guid"] == null)
                    {
                        objAddNewUser.UserName = txtUserName.Text;
                        objAddNewUser.UserEmail = txtUserEmail.Text;
                        objAddNewUser.UserPhone = txtUserPhone.Text;
                        objAddNewUser.UserLocation = txtUserLocation.Text;
                        objAddNewUser.FirstName = txtFirstName.Text;
                        objAddNewUser.LastName = txtLastName.Text;
                        objAddNewUser.Password = PasswordHash.CreateHash(txtPassword.Text);
                        objAddNewUser.IsAdmin = Convert.ToInt32(rdoIsAdmin.SelectedValue);
                        objAddNewUser.UserStatus = Convert.ToInt32(rdoUserStatus.SelectedValue);

                        //if (flUserImage.FileName != "")
                        //{
                        //    string fileName = Path.GetFileName(flUserImage.PostedFile.FileName);
                        //    flUserImage.PostedFile.SaveAs(Server.MapPath("~/Images/UserImages/") + fileName);
                        //    objAddNewUser.UserImage = ConfigurationManager.AppSettings["ImageUrl"].ToString() + "Images/UserImages/" + flUserImage.FileName;
                        //}
                        //else
                        //    objAddNewUser.UserImage = ConfigurationManager.AppSettings["ImageUrl"].ToString() + "Images/UserImages/" + "noimage.png";

                        objAddNewUser.saveUser();
                        lblError.Visible = true;
                        lblError.InnerHtml = "User created successfully";
                    }
                    else
                    {
                        objAddNewUser.GUID = new Guid(Request.QueryString["guid"].ToString());
                        objAddNewUser.UserName = txtUserName.Text;
                        objAddNewUser.UserEmail = txtUserEmail.Text;
                        objAddNewUser.UserPhone = txtUserPhone.Text;
                        objAddNewUser.UserLocation = txtUserLocation.Text;
                        objAddNewUser.FirstName = txtFirstName.Text;
                        objAddNewUser.LastName = txtLastName.Text;
                        objAddNewUser.IsAdmin = Convert.ToInt32(rdoIsAdmin.SelectedValue);
                        objAddNewUser.UserStatus = Convert.ToInt32(rdoUserStatus.SelectedValue);
                        objAddNewUser.Password = PasswordHash.CreateHash(txtPassword.Text);
                        //if (flUserImage.FileName != "")
                        //{
                        //    String path = Server.MapPath(flUserImage.FileName.Replace(ConfigurationManager.AppSettings["ImageUrl"].ToString(), ""));
                        //    if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); }

                        //    string fileName = Path.GetFileName(flUserImage.PostedFile.FileName);
                        //    flUserImage.PostedFile.SaveAs(Server.MapPath("~/Images/UserImages/") + fileName);
                        //    objAddNewUser.UserImage = ConfigurationManager.AppSettings["ImageUrl"].ToString() + "Images/UserImages/" + flUserImage.FileName;
                        //}
                        //else
                        //    objAddNewUser.UserImage = imgUser.ImageUrl == "" ? ConfigurationManager.AppSettings["ImageUrl"].ToString() + "Images/UserImages/" + "noimage.png" : imgUser.ImageUrl;

                        objAddNewUser.updateUser();
                        lblError.Visible = true;
                        lblError.InnerHtml = "User updated successfully";
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.InnerHtml = ex.Message;
            }
            finally
            {
                objAddNewUser = null;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated && Session["User"] == null)
            {
                Response.Redirect("~/Default.aspx?ReturnUrl=~" + Server.UrlEncode(Request.RawUrl));
            }
            if (!IsPostBack)
            {
                lblError.Visible = false;
                lblError.InnerHtml = "";
                //divPassword.Visible = true;
                if (Request.QueryString["guid"] != null)
                {
                    User user = new User();
                    try
                    {

                        DataTable dt = user.getUsersByUserId(new Guid(Request.QueryString["guid"].ToString()));
                        if (dt.Rows.Count > 0)
                        {
                            txtUserName.Text = dt.Rows[0]["UserName"].ToString();
                            txtUserEmail.Text = dt.Rows[0]["UserEmail"].ToString();
                            txtUserPhone.Text = dt.Rows[0]["UserPhone"].ToString();
                            txtUserLocation.Text = dt.Rows[0]["UserLocation"].ToString();
                            txtFirstName.Text = dt.Rows[0]["FirstName"].ToString();
                            txtLastName.Text = dt.Rows[0]["LastName"].ToString();
                            //imgUser.Visible = true;
                            //imgUser.ImageUrl = dt.Rows[0]["UserImage"].ToString();
                            rdoIsAdmin.SelectedValue = dt.Rows[0]["IsAdmin"].ToString();
                            rdoUserStatus.SelectedValue = dt.Rows[0]["UserStat"].ToString();
                           // divPassword.Visible = false;
                        }

                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        user = null;
                    }
                }
            }
        }