private void btnUpdateRate_Click(object sender, EventArgs e) { try { string currency1 = cbCurrency1.SelectedItem as string; string currency2 = cbCurrency2.SelectedItem as string; double rate_d = 1.0; if (!Double.TryParse(txtRate.Text, out rate_d) || rate_d < 0) { MessageBox.Show("Please check the conversion rate, as it does not appear to be valid."); return; } if (currency1 == currency2) { MessageBox.Show("No need to update rate between the same currency"); return; } //Container for our updates HashSet <TTUSAPI.DataObjects.CurrencyExchangeRateProfile> currencyH = new HashSet <TTUSAPI.DataObjects.CurrencyExchangeRateProfile>(); bool foundExchangeRate = false; string exchangeRateName = currency1 + "_" + currency2; foreach (TTUSAPI.DataObjects.CurrencyExchangeRate exchangeRate in m_currencyExchange.Values) // Loop through downloaded exchange rates { if (exchangeRate.Name == exchangeRateName) // Check for match between user selected exchange rate and downloaded exchange rates { TTUSAPI.DataObjects.CurrencyExchangeRateProfile exchangeRateP = new TTUSAPI.DataObjects.CurrencyExchangeRateProfile(exchangeRate); // Make copy of exchange rate exchangeRateP.ExchangeRate = rate_d; // Update the rate currencyH.Add(exchangeRateP); // Add it to the collection foundExchangeRate = true; break; } } if (foundExchangeRate == false) // If the currency exchange rate was not downloaded, we will new to create it from scratch { TTUSAPI.DataObjects.CurrencyExchangeRateProfile exchangeRateP = new TTUSAPI.DataObjects.CurrencyExchangeRateProfile(); // Create empty Exchange Rate exchangeRateP.BaseCurrency = m_lstCurrencies[currency1]; exchangeRateP.TermCurrency = m_lstCurrencies[currency2]; exchangeRateP.ExchangeRate = rate_d; // Set the exchange rate currencyH.Add(exchangeRateP); } ResultStatus res = m_TTUSAPI.UpdateCurrencyExchangeRates(currencyH); if (res.Result == ResultType.SentToServer) // On Success { UpdateStatusBar("Currency exchange rate successfully updated."); } else if (res.Result == ResultType.ValuesUnchanged) // On No Change { UpdateStatusBar("Currency exchange rate unchanged"); } else //On Failure { UpdateStatusBar("Failed to update currency exchange rates, " + res.ErrorMessage); } } catch (Exception ex) { MessageBox.Show("There was an error updating the exchange rates: " + ex.Message); } }
private void btnUpdateRate_Click(object sender, EventArgs e) { try { string currency1 = cbCurrency1.SelectedItem as string; string currency2 = cbCurrency2.SelectedItem as string; double rate_d = 1.0; if (!Double.TryParse(txtRate.Text, out rate_d) || rate_d < 0) { MessageBox.Show("Please check the conversion rate, as it does not appear to be valid."); return; } if (currency1 == currency2) { MessageBox.Show("No need to update rate between the same currency"); return; } //Container for our updates HashSet<TTUSAPI.DataObjects.CurrencyExchangeRateProfile> currencyH = new HashSet<TTUSAPI.DataObjects.CurrencyExchangeRateProfile>(); bool foundExchangeRate = false; string exchangeRateName = currency1 + "_" + currency2; foreach (TTUSAPI.DataObjects.CurrencyExchangeRate exchangeRate in m_currencyExchange.Values) // Loop through downloaded exchange rates { if (exchangeRate.Name == exchangeRateName) // Check for match between user selected exchange rate and downloaded exchange rates { TTUSAPI.DataObjects.CurrencyExchangeRateProfile exchangeRateP = new TTUSAPI.DataObjects.CurrencyExchangeRateProfile(exchangeRate); // Make copy of exchange rate exchangeRateP.ExchangeRate = rate_d; // Update the rate currencyH.Add(exchangeRateP); // Add it to the collection foundExchangeRate = true; break; } } if (foundExchangeRate == false) // If the currency exchange rate was not downloaded, we will new to create it from scratch { TTUSAPI.DataObjects.CurrencyExchangeRateProfile exchangeRateP = new TTUSAPI.DataObjects.CurrencyExchangeRateProfile(); // Create empty Exchange Rate exchangeRateP.BaseCurrency = m_lstCurrencies[currency1]; exchangeRateP.TermCurrency = m_lstCurrencies[currency2]; exchangeRateP.ExchangeRate = rate_d; // Set the exchange rate currencyH.Add(exchangeRateP); } ResultStatus res = m_TTUSAPI.UpdateCurrencyExchangeRates(currencyH); if (res.Result == ResultType.SentToServer) // On Success { UpdateStatusBar("Currency exchange rate successfully updated."); } else if (res.Result == ResultType.ValuesUnchanged) // On No Change { UpdateStatusBar("Currency exchange rate unchanged"); } else //On Failure { UpdateStatusBar("Failed to update currency exchange rates, " + res.ErrorMessage); } } catch (Exception ex) { MessageBox.Show("There was an error updating the exchange rates: " + ex.Message); } }