Example #1
0
        public void TestActiveFound()
        {
            //create an instance of the class we want to create
            clsCustomer ACustomer = new clsCustomer();
            //boolean variable to store the result of the search
            Boolean Found = false;
            //boolean variable to record if data is OK (assume it is)
            Boolean OK = true;
            //create some test data to use with the method
            Int32 AddressNo = 21;

            //invoke the method
            Found = AnAddress.Find(AddressNo);
            //check the property
            if (ACustomer.Active != true)
            {
                OK = false;
            }
            //test to see that the result is correct
            Assert.IsTrue(OK);
        }
        [HttpPost]// take the info from the page and puts it in the database
        public ActionResult Register(RegisterFormVM registerFormVM)
        {
            UserAccount account = registerFormVM.userAccount;
            AnAddress   address = registerFormVM.address;

            Debug.Write(account.toString());
            MySqlParameter param;

            ViewBag.Success = false;

            NewWebSubContext context = HttpContext.RequestServices.GetService(typeof(new_websub.NewWebSubContext)) as NewWebSubContext;

            using (MySqlConnection conn = context.GetConnection())
            {
                try
                {
                    conn.Open();
                    // check if the email and username is unique
                    string       query = "select * from useraccounts where UserName=@username or Email=@email";
                    MySqlCommand cmd   = new MySqlCommand(query, conn);

                    // username
                    param             = new MySqlParameter("@username", account.UserName);
                    param.MySqlDbType = MySqlDbType.VarChar;
                    cmd.Parameters.Add(param);

                    // email
                    param             = new MySqlParameter("@email", account.Email);
                    param.MySqlDbType = MySqlDbType.VarChar;
                    cmd.Parameters.Add(param);

                    MySqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        ViewBag.Message = account.UserName + " or " + account.Email + " is already exist.";
                    }
                    else
                    {
                        // success
                        ViewBag.Success = true;
                    }
                    reader.Close();

                    if (ViewBag.Success)
                    {
                        // insert into address

                        string query1 = "Insert into address(Address1, Address2, City, State, " +
                                        "Zipcode, Country, Address_Created, Address_Modified) values(@Address1, @Address2, @City, @State, " +
                                        "@Zipcode, @Country, @Address_Created, @Address_Modified)";

                        MySqlCommand   cmd1 = new MySqlCommand(query1, conn);
                        MySqlParameter param1;
                        // First name
                        param1             = new MySqlParameter("@Address1", address.Address1);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@Address2", address.Address2);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        // Last name
                        param1             = new MySqlParameter("@City", address.City);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        // User name
                        param1             = new MySqlParameter("@State", address.State);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        // Email
                        param1             = new MySqlParameter("@Zipcode", address.Zipcode);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@Country", address.Country);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);


                        param1             = new MySqlParameter("@Address_Created", DateTime.Now);
                        param1.MySqlDbType = MySqlDbType.DateTime;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@Address_Modified", DateTime.Now);
                        param1.MySqlDbType = MySqlDbType.DateTime;
                        cmd1.Parameters.Add(param1);

                        cmd1.ExecuteNonQuery();
                        query1 = "select * from address";
                        cmd1   = new MySqlCommand(query1, conn);
                        reader = cmd1.ExecuteReader();
                        int addressKey = 0;
                        while (reader.Read())
                        {
                            addressKey = Convert.ToInt32(reader["addresskey"]);
                        }
                        // get address id
                        reader.Close();

                        // insert into useraccounts;

                        query1 = "Insert into useraccounts(FirstName, LastName, UserName, " +
                                 "Email, Password, CompanyName, PhoneNumber, Account_Created, Account_Modified, AddressId) values(@FirstName, @lname, @uname, " +
                                 "@email, @pwd, @company, @phone, @account_created, @account_modified, @AddressId)";

                        cmd1 = new MySqlCommand(query1, conn);

                        // First name
                        param1             = new MySqlParameter("@FirstName", account.FirstName);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        // Last name
                        param1             = new MySqlParameter("@lname", account.LastName);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        // User name
                        param1             = new MySqlParameter("@uname", account.UserName);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        // Email
                        param1             = new MySqlParameter("@email", account.Email);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        byte[] tmpPwd  = ASCIIEncoding.ASCII.GetBytes(account.Password);
                        byte[] tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpPwd);


                        param1             = new MySqlParameter("@pwd", ByteArrayToString(tmpHash));
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@company", account.CompanyName);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@phone", account.PhoneNumber);
                        param1.MySqlDbType = MySqlDbType.VarChar;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@account_created", DateTime.Now);
                        param1.MySqlDbType = MySqlDbType.DateTime;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@account_modified", DateTime.Now);
                        param1.MySqlDbType = MySqlDbType.DateTime;
                        cmd1.Parameters.Add(param1);

                        param1             = new MySqlParameter("@AddressId", addressKey);
                        param1.MySqlDbType = MySqlDbType.Int32;
                        cmd1.Parameters.Add(param1);

                        cmd1.ExecuteNonQuery();
                        ViewBag.Message = "A new member " + account.FirstName + " is added successfully";

                        HttpContext.Session.SetInt32("isLoggedIn", 1);
                        HttpContext.Session.SetString("User", account.Email);

                        return(RedirectToAction(nameof(Index)));
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.Message = ex.ToString();
                }
            }

            return(View());
        }