Пример #1
0
        protected void cAddCustomer_Click(object sender, EventArgs e)
        {
            string firstNameStr = cFirstName.Text.ToString();
            string lastNameStr  = cLastName.Text.ToString();
            string stAddressStr = cAddress.Text.ToString();
            string stateStr     = cState.Text.ToString();
            string zipStr       = cZipCode.Text.ToString();
            string phoneStr     = cPhoneNumber.Text.ToString();
            bool   pageValid    = true;

            // TODO:
            // Add some code validation, WORK IN PROGRESS
            FormValidatorClass    fv = new FormValidatorClass();
            DuplicateCheckerClass dc = new DuplicateCheckerClass();

            // validate if customer already exists
            bool duplicatePerson = dc.AlreadyExists(firstNameStr, lastNameStr, stAddressStr);

            if (duplicatePerson)
            {
                // Customer already exists.
                // TODO: ccreate code to notify AddCustomer.aspx of duplicate entry
                // null the values now so none will be passed to DB
                firstNameStr          = null;
                lastNameStr           = null;
                stAddressStr          = null;
                rfvFirst.ErrorMessage = "Required, person you entered already exists";
                rfvLast.ErrorMessage  = "Required, person you entered already exists";
                rfvFirst.ForeColor    = System.Drawing.Color.Red;
                rfvLast.ForeColor     = System.Drawing.Color.Red;
                pageValid             = false;
            }
            // No need for else, keep validating....

            // validate valid state initials
            bool validState = fv.IsValidState(stateStr);

            if (!validState)
            {
                // invalid state.
                // TODO: create code to notify AddCustomer.aspx of invalid state
                stateStr = null;
                rfvState.ErrorMessage = "Required, enter a valid US state intial (CA, IL. GA)";
                rfvState.ForeColor    = System.Drawing.Color.Red;
                pageValid             = false;
            }
            // keep validating

            // validate phone
            bool validPhone = fv.IsValidPhone(phoneStr);

            if (!validPhone)
            {
                // invalid phone
                // TODO: notify AddCustomer.aspx of invalid phone
                phoneStr = null;
                rfvPhone.ErrorMessage = "Required, enter a valid phone number";
                rfvPhone.ForeColor    = System.Drawing.Color.Red;
                pageValid             = false;
            }

            // validate zip code
            bool validZip = fv.IsValidZip(zipStr);

            if (!validZip)
            {
                // invalid zip
                // TODO: notify AddCustomer.aspx of invalid zip
                zipStr = null;
                rfvZip.ErrorMessage = "Required, enter a valid Zip Code";
                rfvZip.ForeColor    = System.Drawing.Color.Red;
                pageValid           = false;
            }

            // save to DB only if entries are validated
            if (pageValid == true)
            {
                using (LacklusterEntities entity = new LacklusterEntities())
                {
                    customer c = new customer();
                    c.firstName     = firstNameStr;
                    c.lastName      = lastNameStr;
                    c.streetAddress = stAddressStr;
                    c.city          = cCity.Text.ToString();
                    c.state         = stateStr;
                    c.phone         = phoneStr;


                    int zipFromString = 0;
                    int.TryParse(zipStr, out zipFromString);

                    c.zip = zipFromString;

                    /*
                     * if (zipFromString != 0)
                     * {
                     *  c.zip = zipFromString;
                     * }
                     * else
                     * {
                     *  c.zip = 99999;
                     * }
                     */

                    c.active = true;

                    entity.customers.Add(c);
                    entity.SaveChanges();
                }
            }
            else
            {
                // redirect?
            }

            Response.Redirect("~/Management/ManageCustomer.aspx");
        }
Пример #2
0
        protected void eAddEmployee_Click(object sender, EventArgs e)
        {
            /* Edited 4/16/18:
             * passwordPlusSalt is input to Hash algorithm and the output
             * is saved to the DB
             */
            string                lookupSalt       = null;
            string                passwordPlusSalt = null;
            string                passwordString   = ePassword.Text.ToString();
            SaltGenerator         salt             = new SaltGenerator();
            HasherOfPasswords     hash             = new HasherOfPasswords();
            FormValidatorClass    fv = new FormValidatorClass();
            DuplicateCheckerClass dc = new DuplicateCheckerClass();

            /* TODO
             * These variables will be used to check for validation.
             * inputs will be stored in here and checked for validation
             * before being stored as a DB entry.
             *
             */
            string firstNameStr = eFirstName.Text.ToString();
            string lastNameStr  = eLastName.Text.ToString();
            string stAddressStr = eAddress.Text.ToString();
            string stateStr     = eState.Text.ToString();
            string phoneStr     = ePhoneNumber.Text.ToString();
            string zipStr       = eZipCode.Text.ToString();
            string userNameStr  = eUsername.Text.ToString();
            bool   pageValid    = true;

            // validate if person already exists
            bool duplicatePerson = dc.AlreadyExists(firstNameStr, lastNameStr, stAddressStr, userNameStr);

            if (duplicatePerson)
            {
                // then this person already exists in the records
                // TODO: write code in here that alerts the AddEmployee.aspx page of a dulpicate
                // entry attempt. For now, NULL the values so they will not be passed to the DB
                firstNameStr          = null;
                lastNameStr           = null;
                stAddressStr          = null;
                userNameStr           = null;
                rfvFirst.ErrorMessage = "Required, Person you entered already exists";
                rfvLast.ErrorMessage  = "Required, Person you entered already exists";
                rfvFirst.ForeColor    = System.Drawing.Color.Red;
                rfvLast.ForeColor     = System.Drawing.Color.Red;
                pageValid             = false;
            }
            // No need for else, keep validating... If entry does not exist in DB, values
            // won't be nulled. Essentially, values are nulled to force the
            // ASP:RequiredFieldValidator to throw an error.

            // validate state intial
            bool validState = fv.IsValidState(stateStr);

            if (!validState)
            {
                // State is not valid.
                // TODO: write code in here that alerts the AddEmployee.aspx page of an invalid
                // state. For now, NULL the values so they will not be passed to the DB
                stateStr = null;
                rfvState.ErrorMessage = "Required, Enter a valid US state initial (CA, IL, GA)";
                rfvState.ForeColor    = System.Drawing.Color.Red;
                pageValid             = false;
            }
            // No need for else, keep validating...

            // validate phone number
            bool validPhone = fv.IsValidPhone(phoneStr);

            if (!validPhone)
            {
                // Phone number is not valid.
                // TODO: write code that alerts AddEmployee.aspx page of an invalid phone.
                // NULL the value so it will not be passed to the DB.
                phoneStr = null;
                rfvPhone.ErrorMessage = "Required, Enter a valid phone number";
                rfvPhone.ForeColor    = System.Drawing.Color.Red;
                pageValid             = false;
            }

            // validate zip
            bool validZip = fv.IsValidZip(zipStr);

            if (!validZip)
            {
                // Zip Code is not valid.
                // TODO: write code that alerts AddEmployee.aspx page of an invalid zip.
                // NULL the value so it will not be passed to the DB.
                zipStr = null;
                rfvZip.ErrorMessage = "Required, Enter a Valid Zip Code";
                rfvZip.ForeColor    = System.Drawing.Color.Red;
                pageValid           = false;
            }

            // save to DB only if entries are validated.
            if (pageValid == true)
            {
                using (LacklusterEntities entity = new LacklusterEntities())
                {
                    employee em = new employee();
                    em.firstName     = firstNameStr;
                    em.lastName      = lastNameStr;
                    em.streetAddress = stAddressStr;
                    em.city          = eCity.Text.ToString();
                    em.state         = stateStr;
                    em.phone         = phoneStr;
                    em.userName      = userNameStr;

                    lookupSalt       = salt.SaltMe(em.firstName, em.lastName);
                    passwordPlusSalt = passwordString + lookupSalt;
                    em.llv_password  = hash.HashPassword(passwordPlusSalt);
                    em.salt          = lookupSalt;

                    //em.llv_password = ePassword.Text;
                    //eUsername.Text = passwordPlusSalt;

                    em.manager = eIsManager.Checked;
                    em.active  = true;


                    int zipFromString = 0;
                    int.TryParse(zipStr, out zipFromString);

                    em.zip = zipFromString;

                    /*
                     * if (zipFromString != 0)
                     * {
                     *  em.zip = zipFromString;
                     * }
                     * else
                     * {
                     *  em.zip = 99999;
                     * }
                     */
                    entity.employees.Add(em);
                    entity.SaveChanges();
                }
            }
            else
            {
                // redirect?
            }
            Response.Redirect("~/Management/ManageEmployee.aspx");
        }