currency exchange

   #region CurrencyExchangeAPI
        private decimal ExchangeRateFromAPI(decimal amount, string firstCcode, string lastCcode)
        {
            try
            {
                WebClient web = new WebClient();
                string url = string.Format("http://finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=l1", firstCcode.ToUpper(), lastCcode.ToUpper());
                string response = web.DownloadString(url);
                exchangeRate = decimal.Parse(response, System.Globalization.CultureInfo.InvariantCulture);
                return exchangeRate;
            }
            catch (Exception ex)
            {
                exchangeRate = Convert.ToDecimal(64.17);
                return exchangeRate;
            }
        }
        #endregion
        public string getdollar(decimal price)
        {
            decimal apiRate = 0;
            string firstCcode = "USD";
            string lastCcode = "INR";
            decimal amount = 1;
            apiRate = ExchangeRateFromAPI(amount, firstCcode, lastCcode);
            decimal rate = Convert.ToDecimal(price);
            decimal pkr;
            if (decimal.TryParse(apiRate.ToString(), out pkr))
            {
                dollar = rate / pkr;
            }
            return (dollar).ToString("0.00");
        }
        protected void dlproducts_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                LinkButton lkusdprice = (LinkButton)e.Item.FindControl("lkusdprice");
                LinkButton lkPrice = (LinkButton)e.Item.FindControl("lkPrice");
                lkusdprice.Text = "| $" + getdollar(Convert.ToDecimal(lkPrice.Text.ToString().Substring(1, lkPrice.Text.Length - 1)));
            }
        }

Comments

Popular posts from this blog

Create Schema Using C# Asp .Net Dynamically.

FAQ BIND

resgination mail