}        //getAllConversionRates

        //get rates all currencies
        private void threadAllConversionRates(ArrayList alCurrencyNames)
        {
            bool dbStatus = true;

            foreach (CurrencyClass currencyFrom in alCurrencyNames)
            {
                foreach (CurrencyClass currencyTo in alCurrencyNames)
                {
                    if (!currencyFrom.getShortName().Equals(currencyTo.getShortName()) && dbStatus)
                    {
                        RateClass rate = getSingleConversionRate(currencyFrom, currencyTo, false);
                        if (rate != null)
                        {
                            dbStatus = clsDB.saveRate(rate);
                        } //if
                    }     //if
                }         //foreach
            }             //foreach
        }                 //threadAllConversionRates
Exemplo n.º 2
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
        }                 //threadAllConversionRates

        public RateClass getSingleConversionRate(CurrencyClass ccFrom, CurrencyClass ccTo, bool useDB)
        {
            RateClass rate = null;

            try
            {
                //Regular expression to extract conversion rate from HTML
                Regex        regexRate = new Regex("bld>([0-9]*\\.?[0-9]*)");
                WebRequest   request   = WebRequest.Create("https://www.google.com/finance/converter?a=1&from=" + ccFrom.getShortName() + "&to=" + ccTo.getShortName());
                WebResponse  response  = request.GetResponse();
                Stream       data      = response.GetResponseStream();
                string       html      = String.Empty;
                StreamReader reader    = new StreamReader(data);
                //Read one line of HTML at a time until criteria is met then extract rate
                while (reader.Peek() >= 0)
                {
                    html = reader.ReadLine();
                    if (html.Contains("span class=bld"))
                    {
                        Match matchRate = regexRate.Match(html);
                        if (matchRate.Success)
                        {
                            Debug.WriteLine("Rate from " + ccFrom.getShortName() + " to " + ccTo.getShortName() + " is " + matchRate.Groups[1].Value);
                            rate = new RateClass(ccFrom, ccTo, Convert.ToDouble(matchRate.Groups[1].Value), DateTime.Now);
                        } //if
                    }     //if
                }         //while
            }             //try
            catch { }     //website not reachable
            if (useDB)
            {
                if (rate == null)
                {
                    rate = clsDB.getSingleConversionRate(ccFrom, ccTo);
                }//if
                else
                {
                    clsDB.saveRate(rate);
                } //else
            }     //if
            return(rate);
        }         //getSingleConversionRate
Exemplo n.º 4
0
        }     //OpenDatabaseConnection

        //Checks to see if provided entry already exists in the Database
        private bool checkForDuplicate(RateClass rate)
        {
            try
            {
                CurrencyClass   ccFrom        = rate.getFrom();
                CurrencyClass   ccTo          = rate.getTo();
                string          CommandString = "Select * from CurrencyConverter where CurFromShort like '" + ccFrom.getShortName() + "' and CurToShort like '" + ccTo.getShortName() + "'";
                OleDbCommand    cmd           = new OleDbCommand(CommandString, MyConn);
                OleDbDataReader reader        = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
        } //checkForDuplicate
Exemplo n.º 5
0
        }         //Saverate

        //Gets single conversion rate from the database
        public RateClass getSingleConversionRate(CurrencyClass ccFrom, CurrencyClass ccTo)
        {
            RateClass       rate          = null;
            string          CommandString = "Select * from CurrencyConverter where CurFromShort like '" + ccFrom.getShortName() + "' and CurToShort like '" + ccTo.getShortName() + "'";
            OleDbCommand    cmd           = new OleDbCommand(CommandString, MyConn);
            OleDbDataReader reader        = cmd.ExecuteReader();

            if (reader.RecordsAffected > 1)
            {
                //more then one record was returned
            }//endif
            else
            {
                double   rateVal   = 0;
                DateTime dateAdded = DateTime.Now;
                while (reader.Read())
                {
                    rateVal   = Convert.ToDouble(reader["Rate"].ToString());
                    dateAdded = Convert.ToDateTime(reader["DateTime"].ToString());
                } //emdwhile
                rate = new RateClass(ccFrom, ccTo, rateVal, dateAdded);
            }     //endelse
            return(rate);
        }         //getSingleConversionRate
Exemplo n.º 6
0
        }     //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