Exemple #1
0
        private void Save_btn_Click(object sender, EventArgs e)
        {   //apaga este comentario
            string   loan_name      = Name_textBox.ForeColor == Color.Black ? Name_textBox.Text : "";
            string   initial_amount = InitialAmount_textBox.ForeColor == Color.Black ? InitialAmount_textBox.Text : "";
            string   current_debt   = CurrentDebt_textBox.ForeColor == Color.Black ? CurrentDebt_textBox.Text : "";
            string   interest_str   = Interest_textBox.ForeColor == Color.Black ? Interest_textBox.Text : "";
            DateTime term           = DateTime.Parse(Enddate_dateTimePicker.Value.ToString());

            // verify if mandatory fields are filled
            if (loan_name.Equals("") || initial_amount.Equals("") || current_debt.Equals(""))
            {
                ErrorMessenger.EmptyField("Name, Initial Amount and Current Debt");
                return;
            }

            // process inserted values
            double init_amt = DB_API.UnMoneyfy(initial_amount);
            double cur_debt = DB_API.UnMoneyfy(current_debt);
            double interest = 0.0;

            try
            {
                if (interest_str.Equals(""))
                {
                    interest = 0.0;
                }
                else
                {
                    interest = Double.Parse(interest_str);
                }
            }
            catch
            {
                ErrorMessenger.WrongFormat("A numeric textBox");
                return;
            }

            // add loan
            try
            {
                DB_API.InsertLoan(account_id, loan_name, init_amt, cur_debt, term, interest);
            }
            catch (SqlException ex)
            {
                ErrorMessenger.Exception(ex);
                return;
            }

            // upadte listBox with new user
            PopulateLoansListView();
        }
        private static object ExecuteScalar(SqlConnection cnx, SqlCommand cmd)
        {
            object res = null;

            try
            {
                res = cmd.ExecuteScalar();
            }
            catch (SqlException ex)
            {
                ErrorMessenger.Exception(ex);
            }
            finally
            {
                DBdisconnect(cnx);
            }
            return(res);
        }
        // ----------------------------------------------------------------------------------------------
        // QUERY EXECUTION METHODS ----------------------------------------------------------------------
        // ----------------------------------------------------------------------------------------------

        private static int ExecuteNonQuery(SqlConnection cnx, SqlCommand cmd)
        {
            int rows = 0;

            try
            {
                rows = cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                ErrorMessenger.Exception(ex);
            }
            finally
            {
                DBdisconnect(cnx);
            }
            return(rows);
        }
        private static DataTableReader ExecuteReader(SqlConnection cnx, SqlCommand cmd)
        {
            DataTable dt = new DataTable();

            try
            {
                SqlDataReader rdr = cmd.ExecuteReader();
                dt.Load(rdr);
                rdr.Close();
            }
            catch (SqlException ex)
            {
                ErrorMessenger.Exception(ex);
            }
            finally
            {
                DBdisconnect(cnx);
            }
            return(dt.CreateDataReader());
        }
        private void Save_btn_Click(object sender, EventArgs e)
        {
            // get values from textboxes
            string   goal_name = goalname_textBox.ForeColor == Color.Black ? goalname_textBox.Text : "";
            string   amount    = goalamount_textBox.ForeColor == Color.Black ? goalamount_textBox.Text : "";
            int      cat_id    = categories[Categories_comboBox.SelectedItem.ToString()];
            DateTime term      = term_dateTimePicker.Value;

            // verify if field is filled
            if (goal_name.Equals(""))
            {
                ErrorMessenger.EmptyField("Goal name");
                return;
            }

            if (amount.Equals(""))
            {
                ErrorMessenger.EmptyField("Goal amount");
                return;
            }
            double goal_amount = DB_API.UnMoneyfy(amount);

            // add new goal
            if (!Subcategories_comboBox.SelectedItem.ToString().Equals(this.none))
            {
                cat_id = categories[Subcategories_comboBox.SelectedItem.ToString()];
            }
            try
            {
                DB_API.InsertGoal(account_id, cat_id, goal_name, goal_amount, term);
            }
            catch (SqlException ex)
            {
                ErrorMessenger.Exception(ex);
            }

            PopulateGoalsListBox();
        }