0

Here is my ASPX Code:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="Label1" runat="server" Text="From Currency : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
    <asp:Label ID="Label2" runat="server" Text="To Currency : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br />
    <asp:Label ID="Label3" runat="server" Text="Amount : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /><br />
    <asp:Label ID="Label4" runat="server" Text="Rate : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /><br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>

and my C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Decimal amount = 0;
        string fromCurrency = "";
        string toCurrency = "";
        fromCurrency = TextBox1.Text.ToUpper();
        toCurrency = TextBox2.Text.ToUpper();
        amount = Convert.ToDecimal(TextBox3.Text);

        WebClient web = new WebClient();
        Uri uri = new Uri(string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency, toCurrency, amount));
        string response = web.DownloadString(uri);
        Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
        Match match = regex.Match(response);
        string test = match.ToString();
        decimal rate = Convert.ToDecimal(match.Groups[1].Value);
        TextBox4.Text = rate.ToString();
    }
}

I am using this code for Currency Conversion when i click on Button it gives the error "Input string was not in a correct format" in this line "decimal rate = Convert.ToDecimal(match.Groups[1].Value);"

I submit value in

TextBox1 : USD
TextBox1 : INR
TextBox3 : 1
user3834541
  • 161
  • 1
  • 7
  • 27

1 Answers1

5

Here's the code for Google Currency Converter using c#

 public static string CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
    {

        //Grab your values and build your Web Request to the API
        string apiURL = String.Format("https://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString());

        //Make your Web Request and grab the results
        var request = WebRequest.Create(apiURL);

        //Get the Response
        var streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);

        //Grab your converted value (ie 2.45 USD)
        var result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;

        //Get the Result
        return result;
    }
Dipanki Jadav
  • 360
  • 3
  • 7