コード例 #1
0
        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            using (var context = new PnpLoanEntities())
            {
                int userId = int.Parse(Request.QueryString["ID"]);

                //sql version: select top 1 * from SecurityUser
                //             where SecurityUserId=@userId

                //LINQ:
                //var user = (from u in context.SecurityUsers
                //           where u.SecurityUserId == userId)
                //           .FirstOrDefault();

                //Lambda
                var user = context
                           .SecurityUsers
                           .FirstOrDefault(u => u.SecurityUserId == userId);

                user.LastName   = TxtLastname.Text;
                user.FirstName  = TxtFirstname.Text;
                user.MiddleName = TxtMiddlename.Text;
                user.Email      = TxtEmail.Text;
                user.UserTypeId = int.Parse(DdlUserType.SelectedValue);

                context.SaveChanges(); //push the changes to the Database
            }

            Response.Redirect("UserList.aspx");
        }
コード例 #2
0
 private void DisplayUsers()
 {
     using (var context = new PnpLoanEntities())
     {
         GridUser.DataSource = context.SecurityUsers.ToList();
         GridUser.DataBind();
     }
 }
コード例 #3
0
 private void DisplayUserType()
 {
     using (var context = new PnpLoanEntities())
     {
         DdlUserType.DataSource     = context.UserTypes.ToList();
         DdlUserType.DataValueField = "UserTypeId";
         DdlUserType.DataTextField  = "Description";
         DdlUserType.DataBind();
     }
 }
コード例 #4
0
        private void AddUserToDataBase()
        {
            using (var context = new PnpLoanEntities())
            {
                var user = new SecurityUser();

                user.CreatedDate = DateTime.Now;
                user.LastName    = TxtLastname.Text;
                user.FirstName   = TxtFirstname.Text;
                user.MiddleName  = TxtMiddlename.Text;
                user.UserName    = TxtUsername.Text;
                user.Password    = Encryption.GetMD5Hash(TxtPassword.Text);
                user.UserTypeId  = int.Parse(DdlUserType.SelectedValue);
                user.Email       = TxtEmail.Text;

                context.SecurityUsers.Add(user);
                context.SaveChanges();
            }
        }
コード例 #5
0
        private void DisplayUserDetails()
        {
            int id = int.Parse(Request.QueryString["ID"]);

            using (var context = new PnpLoanEntities())
            {
                //SQL: select * from SecurityUser where SecurityUser ID =
                //LINQ
                var selectQuery = from u in context.SecurityUsers
                                  where u.SecurityUserId == id
                                  select u;

                SecurityUser user = selectQuery.FirstOrDefault();

                TxtLastname.Text          = user.LastName;
                TxtFirstname.Text         = user.FirstName;
                TxtMiddlename.Text        = user.MiddleName;
                TxtUsername.Text          = user.UserName;
                TxtEmail.Text             = user.Email;
                DdlUserType.SelectedValue = user.UserTypeId.ToString();
            }
        }
コード例 #6
0
        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            if (GridUser.SelectedRow == null)
            {
                return;                              //this will not execute the codes below anymore
            }
            int userId = int.Parse(GridUser
                                   .SelectedRow
                                   .Cells[1]
                                   .Text);

            using (var context = new PnpLoanEntities())
            {
                var user = context
                           .SecurityUsers
                           .FirstOrDefault(u => u.SecurityUserId == userId);

                context.SecurityUsers.Remove(user);
                context.SaveChanges();
            }
            DisplayUsers();              //refresh the datagrid after deleting
            GridUser.SelectedIndex = -1; //this will 'unselect' all other rows previously selected
        }
コード例 #7
0
        private void DisplayUsers()
        {
            using (var context = new PnpLoanEntities())
            {
                //SQL Equivalent
                //select Lastname, Firstname from Security Users

                //LINQ
                var query = from u in context.SecurityUsers
                            select new {
                    u.SecurityUserId,
                    u.LastName,
                    u.FirstName,
                    u.MiddleName,
                    u.Email,
                    u.UserType.Description
                };

                GridUser.DataSource = query.ToList();
                GridUser.DataBind();
                //GridUser.DataSource = context.SecurityUsers.ToList();
                //GridUser.DataBind();
            }
        }
コード例 #8
0
        private List <string> ValidateInputs()
        {
            var errors = new List <string>();

            if (string.IsNullOrWhiteSpace(TxtLastname.Text))
            {
                errors.Add("Lastname is Required.");
            }

            if (string.IsNullOrWhiteSpace(TxtFirstname.Text))
            {
                errors.Add("Firstname is Required.");
            }

            if (string.IsNullOrWhiteSpace(TxtMiddlename.Text))
            {
                errors.Add("Middlename is Required.");
            }

            if (string.IsNullOrWhiteSpace(TxtUsername.Text))
            {
                errors.Add("Username is Required.");
            }

            if (string.IsNullOrWhiteSpace(DdlUserType.Text))
            {
                errors.Add("Usertype is Required.");
            }

            if (string.IsNullOrWhiteSpace(TxtEmail.Text))
            {
                errors.Add("Email is required.");
            }

            if (string.IsNullOrWhiteSpace(TxtPassword.Text))
            {
                errors.Add("Password is Required.");
            }

            if (string.IsNullOrWhiteSpace(TxtRepeatPassword.Text))
            {
                errors.Add("Re-type Password.");
            }

            if (TxtPassword.Text != TxtRepeatPassword.Text)
            {
                errors.Add("Password Mismatch");
            }

            using (var entityContext = new PnpLoanEntities())
            {
                //select * from SecurityUser where Usernmae='encoder1'

                var users = from u in entityContext.SecurityUsers
                            where u.UserName == TxtUsername.Text.Trim()
                            select u;

                if (users.Count() > 0) //count returns the number of rows from the query
                {
                    errors.Add("Username already exist.");
                }

                //LINQ
                var emailCheck = from u in entityContext.SecurityUsers
                                 where u.Email == TxtEmail.Text.Trim()
                                 select u;

                if (emailCheck.Any())
                {
                    errors.Add("Email already exist.");
                }
            }
            return(errors);
        }