question

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 asked Jackson1990-7147 commented

Read currency rate details available from Web

Hi,
Is there any App/service to get the current currency rate details, from the web, and to flush data into VS project?

dotnet-csharpdotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AddWebSolution-0525 avatar image
0 Votes"
AddWebSolution-0525 answered

You can use any of this following API's :-
free.currencyconverterapi.com
currencylayer.com
currency-api


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered Jackson1990-7147 commented

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



· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
How can VS project make use of the API/service to retrieve the data/details?

0 Votes 0 ·

You don't need any API with free sites like in the sample I posted...

0 Votes 0 ·

Hi,
Thanks a lot. I do not see where you input USD and EUR, to that json to get the rate.

0 Votes 0 ·

I just got the link from https://www.floatrates.com/json-feeds.html
(USD is the first one)


0 Votes 0 ·

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

0 Votes 0 ·

You get code/rate for each link (in USD, except for USD which is in EUR)
For JPY, 0.009 USD

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

See if this works for you, code sample done in VS2019.

109347-figure1.png

 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




figure1.png (22.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.