} //getSingleConversionRate //Gets list of Currencies from the Database public ArrayList getCurrencyNames() { ArrayList tempArray = new ArrayList(); try { string CommandString = "Select * from CurrencyConverter"; OleDbCommand cmd = new OleDbCommand(CommandString, MyConn); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { CurrencyClass ccFrom = new CurrencyClass(reader["CurFromShort"].ToString(), reader["CurFromLong"].ToString()); CurrencyClass ccTo = new CurrencyClass(reader["CurToShort"].ToString(), reader["CurToLong"].ToString()); if (!tempArray.Contains(ccFrom)) { tempArray.Add(ccFrom); }//endif if (!tempArray.Contains(ccTo)) { tempArray.Add(ccTo); } //endif } //endwhile } //endtry catch { } return(tempArray); }//getCurrencyNames
public RateClass(CurrencyClass from, CurrencyClass to, double rate, DateTime dt) { ccFrom = from; ccTo = to; dblRate = rate; dtDateTime = dt; }//RateClass
}//getLongName public override bool Equals(object o) { if (o == null || o.GetType() != typeof(CurrencyClass)) { return(false); } CurrencyClass currency = (CurrencyClass)o; return(currency.getLongName().Equals(this.strLongName) && currency.getShortName().Equals(this.strShortName)); }//Equals
}//WebParserClass //This function will parse a web page and retrieve all currency names public ArrayList getCurrencyNames() { //Regular expression to extract currency names from HTML Regex regexOPTION = new Regex("</?\\w+\\s+\\w+=\"(.*)\">(.*)</\\w+>"); ArrayList tempArray = new ArrayList(); try { //get HTML from the site WebRequest request = WebRequest.Create("https://www.google.com/finance/converter"); WebResponse response = request.GetResponse(); Stream data = response.GetResponseStream(); string html = String.Empty; StreamReader reader = new StreamReader(data); bool parse = false; //read one line at a time untill criteria is met. Once met use regular expression to extract needed information while (reader.Peek() >= 0) { html = reader.ReadLine(); if (html.Contains("select name=from")) { parse = true; }//if else if (html.Contains("</select>") && parse) { parse = false; }//elseif else if (parse) { Match matchOption = regexOPTION.Match(html); CurrencyClass currency = new CurrencyClass(matchOption.Groups[1].Value, matchOption.Groups[2].Value); tempArray.Add(currency); } //elseif } //while } //try catch { } //website not reachable //If website parsing failed, fail over to Database if (tempArray.Count == 0) { try { tempArray = clsDB.getCurrencyNames(); } catch { }//Database not reachable } //if return(tempArray); } //getCurrencyNames
//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
} //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
} //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
} //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