private void loginButton_Click(object sender, EventArgs e)
        {
            string inputUsername = loginTextbox.Text;
            string inputPassword = passwordTextbox.Text;

            DatabaseHandler dh = new DatabaseHandler();

            dh.RemoveAllUnplacedOrders();

            bool isDataCorrect = dh.CheckLoginData(inputUsername, inputPassword);

            if (isDataCorrect == true)
            {
                form_MainUserWindow MUW = new form_MainUserWindow(inputUsername);
                this.Hide();
                MUW.ShowDialog();
                this.Close();
            }

            else
            {
                form_SystemMessage wrongCredentials = new form_SystemMessage("Unable to log in.", "Check your login and password.");
                loginTextbox.Clear();
                passwordTextbox.Clear();
                this.ActiveControl = loginTextbox;
            }
        }
Exemplo n.º 2
0
        public int UpdateClientField(string columnName, string value, string username)
        {
            int           affected   = 0;
            SqlConnection connection = new SqlConnection(this.connectionString);
            string        update     = "UPDATE Clients " +
                                       $"SET {columnName} = @value " +
                                       "WHERE Username = @username ";

            try
            {
                affected = connection.Execute(update, new { value = value, username = username });
            }
            catch (Exception DatabaseHandlerException) { Console.WriteLine(DatabaseHandlerException.Message); }

            if (affected == 1)
            {
                form_SystemMessage success = new form_SystemMessage("Success!", $"Data has been updated!");
            }
            else
            {
                form_SystemMessage failure = new form_SystemMessage("Failure!", "Something went wrong.");
            }

            connection.Close();
            return(affected);
        }
        private void shoppingKartView_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == shoppingKartView.Columns["Quantity"].Index)
            {
                int quantity = productsInKart
                               .Where(x => x.ProductID == Convert.ToInt32(shoppingKartView[0, e.RowIndex].Value))
                               .Select(x => x.Quantity).Single();
                try
                {
                    if (Convert.ToInt32(shoppingKartView["Quantity", e.RowIndex].Value) > quantity)
                    {
                        form_SystemMessage alert = new form_SystemMessage("Sorry.", "Please check available product amount.");

                        int productID = Convert.ToInt32(shoppingKartView[0, e.RowIndex].Value);

                        shoppingKartView[e.ColumnIndex, e.RowIndex].Value =
                            shoppingKartList
                            .Where(x => x.ProductID == productID)
                            .Select(x => x.Quantity).Single <int>();
                    }
                    else
                    {
                        int productID   = (int)shoppingKartView[0, e.RowIndex].Value;
                        int newQuantity = Convert.ToInt32(shoppingKartView["Quantity", e.RowIndex].Value);
                        dh.UpdateQuantityInKart(_shoppingKartID, productID, newQuantity);
                        BuildProductsList();
                        ComputeOrderValue();
                    }
                }
                catch (Exception gridException) { Console.WriteLine(gridException.Message); }
            }
        }
Exemplo n.º 4
0
        //BUTTONS
        private void createAccountButton_Click(object sender, EventArgs e)
        {
            string companyName = @companyNameTB.Text;

            if (companyNameTB.Text == DefaultTexts._companyName)
            {
                companyName = "No details.";
            }

            string finalUsername = @loginTB.Text;

            string finalPassword;

            if (passwordTB_create.Text == passwordTB_repeat.Text)
            {
                finalPassword = @passwordTB_repeat.Text;
            }
            else
            {
                throw new Exception("Wrong password exception.");
            }

            createAccountButton.Enabled = false;

            DatabaseHandler dh = new DatabaseHandler();

            try
            {
                int affected = dh.AddUser(new Client()
                {
                    FirstName      = @nameTB.Text,
                    SecondName     = @surnameTB.Text,
                    CompanyName    = @companyName,
                    Email          = @mailTB.Text,
                    PhoneNumber    = @phoneTB.Text,
                    Country        = @countryTB.Text,
                    City           = @cityTB.Text,
                    Street         = @streetTB.Text,
                    Postcode       = @postcodeTB.Text,
                    BuildingNumber = @buildingTB.Text,
                    Username       = finalUsername,
                    Password       = finalPassword,
                }
                                          );

                if (affected == 1)
                {
                    form_SystemMessage message = new form_SystemMessage
                                                     ("Success!", "Your account has been created!", this);
                }
            }
            catch (Exception whileInserting)
            {
                Console.WriteLine(whileInserting.Message);
                form_SystemMessage alert = new form_SystemMessage
                                               ("Failure!", "Your account wasn't created. \nTry again please.");
            }
        }
        private void placeOrderButton_Click(object sender, EventArgs e)
        {
            shoppingKartList = dh.GetKartList(_shoppingKartID);

            int _broken = 0;
            int _passed = 0;

            //try updating values
            foreach (OrderDetail item in shoppingKartList)
            {
                int updateResult = dh.UpdateProductQuantityBasedOnKart
                                       (item.ProductID, item.Quantity);
                if (updateResult == 1)
                {
                    _passed += 1;
                }
                else
                {
                    _broken = 1;
                    break;
                }
            }

            //if an error occurs, values are returned to the previous state
            if (_broken == 1)
            {
                foreach (OrderDetail item in shoppingKartList)
                {
                    int reverse = dh.UpdateProductQuantityBasedOnKart(item.ProductID, (-1) * item.Quantity);

                    if (reverse == 1)
                    {
                        _passed -= 1;
                    }
                    if (_passed == 0)
                    {
                        break;
                    }
                }
            }
            else if (_broken == 0)
            {
                int result = dh.ConfirmOrder(_accountOwner.ClientID, _shoppingKartID);
                if (result == 1)
                {
                    form_SystemMessage success = new form_SystemMessage("Success!", "Your order is being prepared!", this);
                }
                else
                {
                    form_SystemMessage success = new form_SystemMessage("Failure.", "Something went wrong, please try again.");
                }
            }
        }
Exemplo n.º 6
0
        //ORDER DETAILS RELATED METHODS
        public int AddToKartIfNotExists(int shoppingKartID, int productID)
        {
            string insert =
                "INSERT INTO OrderDetails([OrderID],[ProductID],[Price],[Quantity]) " +
                "VALUES (@orderID, @productID, @price, @quantity) ";

            string select = "SELECT COUNT(*) FROM OrderDetails " +
                            "WHERE OrderID = @kartID " +
                            "AND ProductID = @productID";
            //prevents duplicates
            Product requested = this.GetProduct(productID);
            int     affected  = 0;

            if (requested != null)
            {
                try
                {
                    SqlConnection connection = new SqlConnection(this.connectionString);

                    int existing = connection.QuerySingle <int>(select,
                                                                new {
                        kartID    = shoppingKartID,
                        productID = productID
                    });

                    if (existing == 0)
                    {
                        affected = connection.Execute(insert, new
                        {
                            orderID   = shoppingKartID,
                            productID = requested.ProductID,
                            price     = requested.Price,
                            quantity  = 1
                        });
                    }
                    connection.Close();
                }
                catch (Exception DatabaseHandlerException)
                {
                    Console.WriteLine(DatabaseHandlerException.Message);
                }
            }
            else
            {
                form_SystemMessage failure = new form_SystemMessage("Sorry.", "We couldn't find this item.");
            }
            return(affected);
        }
Exemplo n.º 7
0
        private void ViewSearchResult(List <Product> searchResult)
        {
            AddColumns();

            if (searchResult != null)
            {
                foreach (Product product in searchResult)
                {
                    searchResultView.Rows.Add(
                        product.ProductID,
                        product.Category,
                        product.Name,
                        product.Manufacturer,
                        Math.Round(product.Price, 2)
                        );
                }
            }
            else
            {
                form_SystemMessage failure = new form_SystemMessage("Sorry.", "Something went wrong.");
            }
        }