Read currency rate details available from Web

Peter_1985 2,466 Reputation points
2021-06-25T02:14:50.35+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,201 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AddWebSolution 161 Reputation points
    2021-06-25T05:17:41.893+00:00

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

    0 comments No comments

  2. Castorix31 81,461 Reputation points
    2021-06-25T05:57:08.727+00:00

    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  
    

  3. Karen Payne MVP 35,031 Reputation points
    2021-06-25T12:14:26.587+00:00

    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  
    
    0 comments No comments