locked
add currency converter RRS feed

  • Question

  • User-1327394822 posted

    I am using the below code to change convert price to other currency.

    1) how to get live currency?

    2) does asp.net have any builtin support for this?

    3) is the method I am using good?

    @{
        
        var currency = "INR";
        var symbol = "Rs. ";
        double price = 65;
        
        if(!Request["currency"].IsEmpty()){
            Response.Cookies["currency"].Value = Request["currency"];
            Response.Cookies["currency"].Expires = DateTime.Now.AddDays(7);
        }
        if(Response.Cookies["currency"] != null){
            currency = Response.Cookies["currency"].Value;
        }
        if(!IsPost){
             switch(currency){
                case "INR":
                price = price;
                break;
    
                case "USD":
                price = price / 65;
                symbol = "$";
                break;
    
                case "GBP":
                price = price / 100;
                symbol = "&";
                break;
    
                case "DHS":
                price = price / 17.4;
                symbol = "DHS";
                break;
    
            }
        }
       
    }
    <div class="container">
        <form style="max-width: 300px; margin-top: 50px;" class="form-inline">
            <select name="currency" class="form-control resizedInput">
                <option value="INR" @(currency == "INR" ? "selected =\"selected\"" : " ")>INR</option>
                <option value="USD"@(currency == "USD" ? "selected =\"selected\"" : " ")>USD</option>
                <option value="GBP" @(currency == "GBP" ? "selected =\"selected\"" : " ")>GBP</option>
                <option value="DHS" @(currency == "DHS" ? "selected =\"selected\"" : " ")>DHS</option>
            </select>
            <input type="submit" class="btn btn-default changingbutton" value="go" />
        </form> 
    
    <div>
        Price in @currency = @symbol @String.Format("{0:0.00}", price)
    </div>
    </div>

    Wednesday, December 16, 2015 9:16 PM

Answers

  • User325035487 posted

    Free api here - yahoo

    If you want as Json then go here

    For example : USD-INR AND EUR-INR - here

    use the package console manager to get the Newtonsoft JSON serielizer using this command

    PM> Install-Package Newtonsoft.Json

    Then

    @using System;
    @using System.Collections.Generic;
    @using System.Linq;
    @using Newtonsoft.Json.Linq;
    @using System.Text;
    @using System.Net;
    @using System.Web;
    @{
        StringBuilder theWebAddress = new StringBuilder();
        theWebAddress.Append("http://query.yahooapis.com/v1/public/yql?");
        theWebAddress.Append("q=" + System.Web.HttpUtility.UrlEncode("select * from yahoo.finance.xchange where pair in ('USDINR', 'EURINR')"));
        theWebAddress.Append("&format=json");
        theWebAddress.Append("&diagnostics=false");
    
        string results = "";
    
        using (WebClient wc = new WebClient())
        {
            results = wc.DownloadString(theWebAddress.ToString());
        }
    
        JObject dataObject = JObject.Parse(results);
        JArray jsonArray = (JArray)dataObject["query"]["results"]["rate"];
    
        foreach (var rate in jsonArray)
        {
            <h2>@rate["Name"]</h2>
            <p>Exchange Rate : @rate["Rate"] on @rate["Date"] at @rate["Time"]</p>
            Environment.NewLine;
        }
    }

    Hope you can go from there..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 16, 2015 9:36 PM

All replies

  • User325035487 posted

    Free api here - yahoo

    If you want as Json then go here

    For example : USD-INR AND EUR-INR - here

    use the package console manager to get the Newtonsoft JSON serielizer using this command

    PM> Install-Package Newtonsoft.Json

    Then

    @using System;
    @using System.Collections.Generic;
    @using System.Linq;
    @using Newtonsoft.Json.Linq;
    @using System.Text;
    @using System.Net;
    @using System.Web;
    @{
        StringBuilder theWebAddress = new StringBuilder();
        theWebAddress.Append("http://query.yahooapis.com/v1/public/yql?");
        theWebAddress.Append("q=" + System.Web.HttpUtility.UrlEncode("select * from yahoo.finance.xchange where pair in ('USDINR', 'EURINR')"));
        theWebAddress.Append("&format=json");
        theWebAddress.Append("&diagnostics=false");
    
        string results = "";
    
        using (WebClient wc = new WebClient())
        {
            results = wc.DownloadString(theWebAddress.ToString());
        }
    
        JObject dataObject = JObject.Parse(results);
        JArray jsonArray = (JArray)dataObject["query"]["results"]["rate"];
    
        foreach (var rate in jsonArray)
        {
            <h2>@rate["Name"]</h2>
            <p>Exchange Rate : @rate["Rate"] on @rate["Date"] at @rate["Time"]</p>
            Environment.NewLine;
        }
    }

    Hope you can go from there..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 16, 2015 9:36 PM
  • User-1327394822 posted

    thank you. Can you please show me an example using the first one i.e xml api?

    Thursday, December 17, 2015 5:55 AM
  • Thursday, December 17, 2015 10:33 AM