Asp.net real-time currency converter using api (google, yahoo), web service, and jquery ajax – codeproject bitcoin in dollars

• home• articles

• chapters and sections >

• search

• latest articles

• latest tips/tricks

• top articles

• beginner articles

• technical blogs

• posting/update guidelines

• article help forum

• article competition

• submit an article or tip

• post your blog

• quick answers QA

• ask a question about this

Article

• ask a question

• view unanswered questions

• view all questions…

• C# questions

• ASP.NET questions

• VB.NET questions

• SQL questions

• javascript questions

• discussions forums

• all message boards…

• application lifecycle >

• running a business

• sales / marketing

• collaboration / beta testing

• work issues

• design and architecture

• ASP.NET

• javascript

• C / C++ / MFC >

• ATL / WTL / STL

• managed C++/CLI

• C#

• free tools

• objective-C and swift

• database

• hardware devices >

• system admin

• hosting and servers

bitcoin in dollars

• java

• .NET framework

• android

• ios

• mobile

• sharepoint

• silverlight / WPF

• visual basic

• web development

• site bugs / suggestions

• spam and abuse watch

• features

• competitions

• news

• the insider newsletter

• the daily build newsletter

• newsletter archive

• surveys

• product showcase

• research library

• codeproject stuff

• community lounge

• who’s who

• most valuable professionals

• the lounge

• the insider news

• the weird the wonderful

• the soapbox

• press releases

• non-english language

>

• general indian topics

• general chinese topics

• help

• what is ‘codeproject’?

• general FAQ

• ask a question

• bugs and suggestions

• article help forum

• site map

• advertise with us

• about our advertising

• employment opportunities

• about us

Introduction

Google, yahoo provides many apis that can be used in our applications.Bitcoin in dollars here I’m going to show a real-time currency converter using google’s/yahoo’s API.

Click here to view available apis from google.

Click here to view available apis from yahoo.

For this article I have created a web application from scratch that will pull data from google/yahoo and will display in user interface using jquery ajax and web service. For your reference I have attached the source code with this article. Your votes , suggestions and feedback are highly appreciated to improve the quality of this and upcoming articles, please don’t forget.

Let me try to explain the steps for you… background

Google is using their own hidden API for showing the real-time currency rate in their search engine. Why it’s hidden,because this API doesn’t come with an official document. If you haven’t checked this service yet then click here or else see the below given screen-shot.Bitcoin in dollars

Yahoo’s real-time currency converter API will give you a .Csv file with the currency conversion rate. Click here to see yahoo’s online currency converter or else see the below given screen-shot.

Using the code

In this project the main components are ASPX, CSS, jquery and web service.The currency converter solution will look like below screen.

HTML/ASPX

As you can see from below given screen shot , the UI contains very less controls.Two dropdowns, one text box and one button, the result is just displayed in a table cell .

Here textbox is used to capture the amount which needs to be converted to the desired currency from the current currency and two drop downs are prefilled with the currency code and country names. Once you click on the convert button it will call a jquery ajax method which calls a webmethod internally.Bitcoin in dollars you can get the country list and codes from the attached file. CSS

I just created this CSS for a better look and feel to the UI. . Main

{

Font-family : tahoma; font-size : 12px; border : solid 1px #03325C; border-collapse : collapse; color : #094D23; width : 400px;

}

. Main th

{

Border-color : #03325C #03325C #03325C #03325C; border-style : solid solid solid solid; border-width : 1px 1px 1px 1px; color : #EDF7FF; padding : 4px 5px 4px 10px; vertical-align : bottom; text-align : center; background-color : #0E3252;

}

. Result

{

Border-color : #989898 #989898 #989898 #989898; border-style : solid solid solid solid; border-width : 1px 1px 1px 1px; color : #1A0229; padding : 4px 5px 4px 10px; vertical-align : bottom; text-align : left; background-color : #C7CCD1; font-weight : bold;

}

. Center

{

Text-align : center;

bitcoin in dollars

}

. Controls

{

Font-family : tahoma; font-size : 12px; color : #032610;

} jquery

Here I’m explaining the jquery ajax code that used to call the web service which returns rate from google’s API or yahoo’s API.

$(document).Ready( function () {

$( ‘ #submit’).Click( function () {

Var errormsg = ;

Var amount = $( ‘ #txtamount’).Val();

Var from = $( ‘ #drpfrom’).Val();

Var to = $( ‘ #drpto’).Val();

$.Ajax({ type: POST,

Url: webservice.Asmx/convertgoog,

Data: {amount: + amount + ,fromcurrency:’ + from + ‘,tocurrency:’ + to + ‘},

ContentType: application/json; charset=utf-8,

DataType: json,

BeforeSend: function () {

$( ‘ #results’).Html( converting…);

},

Success: function (data) {

$( ‘ #results’).Html(amount + ‘ ‘ + from + ‘ =’ + data.D.ToFixed( 2) + ‘ ‘ + to);

},

Error: function (jqxhr, exception) {

If (jqxhr.Status === 0) {

bitcoin in dollars

Errormsg = ‘ not connect.\n verify network.’; ;

} else if (jqxhr.Status == 404) {

Errormsg = ‘ requested page not found. [404]’; ;

} else if (jqxhr.Status == 500) {

Errormsg = ‘ internal server error [500].’; ;

} else if (exception === ‘ parsererror’) {

Errormsg = ‘ requested JSON parse failed.’; ;

} else if (exception === ‘ timeout’) {

Errormsg = ‘ time out error.’; ;

} else if (exception === ‘ abort’) {

Errormsg = ‘ ajax request aborted.’; ;

} else {

Errormsg = ‘ uncaught error.’;

}

$( ‘ #results’).Html(errormsg);

$( ‘ click here for more details’).Click( function () {

Alert(jqxhr.ResponseText);

}).AppendTo( ‘ #results’);

}

});

});

});

In the above code, the click() event is tied to the submit button and jquery val() function is used to retrieve the control values and its passed to the webservice method. By using $.Ajax we are making a jquery ajax call to a .NET webservice.Bitcoin in dollars here before initiating the ajax call I’m setting the result value as converting… Using the beforesend setting of $.Ajax. If the call is success then the currency rate will display in the result cell else the error will display. Google

We can call the google web method by setting the url setting of jquery ajax call as below. Url: webservice.Asmx/convertgoog, yahoo

We can call the yahoo web method by setting the url setting of jquery ajax call as below.

Url: webservice.Asmx/convertyhoo,

When the element with ID “submit” (convert button) is clicked then this method will get called. By using val() function of jquery the html controls values are stored to a variable and that variable is passed to the web service method. We are calling the jquery ajax call by the $.Ajax , the web service name and method are mentioned in url setting of jquery.Ajax() function.Bitcoin in dollars so when we click the result cell will display “converting…”, if the call is success then the expected output will be shown in the result otherwise it will display an error description and a hyperlink that will show the error cause in detail. I have tried to handle some of the errors in jquery ajax call.

Error setting of jquery ajax call.

This block is used to catch the jquery ajax call to the web method, this code will set the error description and will add a hyperlink for more error details, you can see the sample output below.

Error: function (jqxhr, exception) {

If (jqxhr.Status === 0) {

Errormsg = ‘ not connect.\n verify network.’; ;

} else if (jqxhr.Status == 404) {

Errormsg = ‘ requested page not found. [404]’; ;

} else if (jqxhr.Status == 500) {

Errormsg = ‘ internal server error [500].’; ;

bitcoin in dollars

} else if (exception === ‘ parsererror’) {

Errormsg = ‘ requested JSON parse failed.’; ;

} else if (exception === ‘ timeout’) {

Errormsg = ‘ time out error.’; ;

} else if (exception === ‘ abort’) {

Errormsg = ‘ ajax request aborted.’; ;

} else {

Errormsg = ‘ uncaught error.’;

}

$( ‘ #results’).Html(errormsg);

$( ‘ click here for more details’).Click( function () {

Alert(jqxhr.ResponseText);

}).AppendTo( ‘ #results’);

}

Webservice

Here the convert method will accept three input parameters. The parameters are amount, fromcurrency and tocurrency google

ConvertGOOG web method is used to handle the google API call to return the currency rate. The recieved response is parsed and only the rate part is returned from this function. For your quick reference I have given the debugging screen shot below.So that you can see the response and the url passed from this method.Bitcoin in dollars yahoo

ConvertYHOO web method is used to handle the yahoo API call to return the currency rate. The recieved response is parsed and only the rate part is returned from this function. For your quick reference I have given the debugging screen shot below.So that you can see the response and the url passed from this method.

The expected format of the rate is a proper decimal number, if any other format (eg: 9 433.56 – space came in between number) comes then we need to handle the same in web method. Summary

I hope this article helped you to understand real-time currency converter using apis in ASP.NET.

Please give your valuable votes , suggestions and feedback for further improvements. Thanks for reading.