Example #1
0
        //Method used for saving rates into the database
        public bool saveRate(RateClass rate)
        {
            bool          Successful = false;
            CurrencyClass ccFrom     = rate.getFrom();
            CurrencyClass ccTo       = rate.getTo();

            //If entry does not exist insert it
            if (checkForDuplicate(rate) == false)
            {
                try
                {
                    string       insert = "INSERT INTO CurrencyConverter ([CurFromLong], [CurFromShort], [CurToLong], [CurToShort], [Rate], [DateTime]) VALUES (@CurFromLong, @CurFromShort, @CurToLong, @CurToShort, @Rate, @DateTime);";
                    OleDbCommand cmd    = new OleDbCommand(insert, MyConn);
                    cmd.Parameters.AddWithValue("@CurFromLong", ccFrom.getLongName());
                    cmd.Parameters.AddWithValue("@CurFromShort", ccFrom.getShortName());
                    cmd.Parameters.AddWithValue("@CurToLong", ccTo.getLongName());
                    cmd.Parameters.AddWithValue("@CurToShort", ccTo.getShortName());
                    cmd.Parameters.AddWithValue("@Rate", rate.getRate().ToString());
                    cmd.Parameters.AddWithValue("@DateTime", rate.getTimeDate().ToString());
                    cmd.ExecuteNonQuery();
                    Successful = true;
                } //endtry
                catch {}
            }     //endif
            //if entry does exist, update it
            else
            {
                try
                {
                    string       update = "UPDATE CurrencyConverter SET [Rate]=@Rate, [DateTime]=@DateTime WHERE CurFromShort LIKE '" + ccFrom.getShortName() + "' AND CurToShort LIKE '" + ccTo.getShortName() + "'";
                    OleDbCommand cmd    = new OleDbCommand(update, MyConn);
                    cmd.Parameters.AddWithValue("@Rate", rate.getRate().ToString());
                    cmd.Parameters.AddWithValue("@DateTime", rate.getTimeDate().ToString());
                    cmd.ExecuteNonQuery();
                    Successful = true;
                }//endtry
                catch
                {
                    //returns false
                } //endcatch
            }     //endelse
            return(Successful);
        }         //Saverate
        }     //frmMain_Load

        //Converts currencies when the button is clicked
        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (cmbFrom.SelectedItem == cmbTo.SelectedItem)
            {
                txtTo.Text   = txtFrom.Text;
                lblAsOf.Text = "As Of " + DateTime.Now;
            }//if
            else
            {
                CurrencyClass ccFrom = new CurrencyClass((string)((ComboboxItem)cmbFrom.SelectedItem).Value, ((ComboboxItem)cmbFrom.SelectedItem).Text);
                CurrencyClass ccTo   = new CurrencyClass((string)((ComboboxItem)cmbTo.SelectedItem).Value, ((ComboboxItem)cmbTo.SelectedItem).Text);
                RateClass     rate   = webParser.getSingleConversionRate(ccFrom, ccTo, true);
                Debug.WriteLine(rate.ToString());
                if (rate != null)
                {
                    txtTo.Text   = "" + (Convert.ToDouble(txtFrom.Text) * rate.getRate());
                    lblAsOf.Text = "As Of " + rate.getTimeDate().ToString();
                }//if
                else
                {
                    MessageBox.Show("Could not retrieve conversion rate for " + ccFrom.getShortName() + " to " + ccTo.getShortName() + " at this time!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                } //else
            }     //else
        }         //btnConvert_Click