Hi,
Is there any App/service to get the current currency rate details, from the web, and to flush data into VS project?
Hi,
Is there any App/service to get the current currency rate details, from the web, and to flush data into VS project?
You can use any of this following API's :-
free.currencyconverterapi.com
currencylayer.com
currency-api
You can use WebRequest
Quick test in C# =>
WebRequest request = WebRequest.Create("http://www.floatrates.com/daily/usd.json");
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
string sRateBegin = "\"rate\":";
string sRateEnd = ",";
string sRate = responseFromServer.Substring(responseFromServer.IndexOf(sRateBegin) + sRateBegin.Length);
sRate = sRate.Substring(0, sRate.IndexOf(sRateEnd));
Console.WriteLine("1 USD = {0} EUR", sRate);
reader.Close();
response.Close();
I get :
1 USD = 0.83791020896443 EUR
Hi,
How can VS project make use of the API/service to retrieve the data/details?
You don't need any API with free sites like in the sample I posted...
Hi,
Thanks a lot. I do not see where you input USD and EUR, to that json to get the rate.
I just got the link from https://www.floatrates.com/json-feeds.html
(USD is the first one)
There are different links below for different currency. How do we know which other currency is being compared against USD or JPY?
http://www.floatrates.com/daily/usd.json
http://www.floatrates.com/daily/jpy.json
You get code/rate for each link (in USD, except for USD which is in EUR)
For JPY, 0.009 USD
See if this works for you, code sample done in VS2019.

Imports System.Xml
Partial Public Class Form1
Inherits Form
Private ReadOnly ExchangeRateToEuro As Dictionary(Of String, Decimal) =
New Dictionary(Of String, Decimal)()
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
LoadRates()
FromCurrency.SelectedIndex = 0
ToCurrency.SelectedIndex = 0
Catch ex As Exception
ConvertButton.Enabled = False
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub LoadRates()
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")
UpdateLabel.Text = $"Rates Updated: {xmlDoc.DocumentElement.ChildNodes(2).ChildNodes(0).Attributes("time").Value}"
For Each node As XmlNode In xmlDoc.DocumentElement.ChildNodes(2).ChildNodes(0).ChildNodes
ExchangeRateToEuro.Add(node.Attributes("currency").Value, Decimal.Parse(node.Attributes("rate").Value))
FromCurrency.Items.Add(node.Attributes("currency").Value)
ToCurrency.Items.Add(node.Attributes("currency").Value)
Next
End Sub
Private Sub ConvertButton_Click(sender As Object, e As EventArgs) Handles ConvertButton.Click
If Not ConvertButton.IsHandleCreated Then Return
Dim conversionNum As Decimal = (amount.Value / ExchangeRateToEuro(FromCurrency.Text)) * ExchangeRateToEuro(ToCurrency.Text)
ConvertValue.Text = amount.Value & " " &
FromCurrency.Text & " = " &
conversionNum.ToString("0.00") & " " &
ToCurrency.Text
End Sub
Private Sub amountLeave(sender As Object, e As EventArgs) Handles amount.Leave
If Not amount.IsHandleCreated Then Return
If amount.Text = String.Empty Then
amount.Value = amount.Minimum
amount.Text = amount.Value.ToString()
End If
End Sub
End Class
11 people are following this question.