//INSERT EXPENSE command here private void addExpense_Button_Click(object sender, EventArgs e) { //check the user name associated with this command if (userName.TextLength == 0) { userName.Text = "Admin"; UserName = userName.Text; } else { UserName = userName.Text; } try { //This is my connection string i have assigned the database file address path string MyConnection2 = ConnectionConstant; //This is my insert query in which i am taking input from the user through windows forms if (Double.TryParse(amountBox.Text, out double result)) { //get the amount of this expense in ten years double tenYearsAmount = 0.0; if (oneTime_Button.Checked) { tenYearsAmount = WealthEstimator.EstimateTenYearsAmount_OneTime(Double.Parse(amountBox.Text)); tenYears_Amount.Text = tenYearsAmount.ToString("C0"); } if (monthlyButton.Checked) { tenYearsAmount = WealthEstimator.EstimateTenYearsAmount_Monthly(Double.Parse(amountBox.Text)); tenYears_Amount.Text = tenYearsAmount.ToString("C0"); } string Query = $"INSERT INTO `U053QS`.`expenses` (`user`, `expense`, `date`, `ten_year`, `Description`) VALUES (\"{UserName}\", '{amountBox.Text}', CURRENT_DATE(), '{tenYearsAmount.ToString("0")}', '{description_Box.Text}');"; //This is MySqlConnection here i have created the object and pass my connection string. MySqlConnection MyConn2 = new MySqlConnection(MyConnection2); //This is command class which will handle the query and connection object. MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2); MySqlDataReader MyReader2; MyConn2.Open(); MyReader2 = MyCommand2.ExecuteReader(); // Here our query will be executed and data saved into the database. MessageBox.Show("Expense added"); MyConn2.Close(); } else { MessageBox.Show("The data is in incorrect format, NOT inserted!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void oneTime_Button_CheckedChanged(object sender, EventArgs e) { try { tenYears_Amount.Text = WealthEstimator.EstimateTenYearsAmount_OneTime(Double.Parse(amountBox.Text)).ToString("C0"); } catch { } }