Sunday, January 6, 2013

Simple steps to create Currency converter application using Asp.net and c#

In this article i would like to explain how to create a currency converter application using Asp.net and c#.Here we are taking the help of webservice.net in order to get the live currency values through webservice. So lets start how to implement currency converter application.



facebook_login_button





First,Add one aspx page Ex: Default.aspx Next, Drag and Drop two dropdownlists,two text boxes and one button in aspx page. Controls : 2 Dropdownlists - One for Dropdownlist to bind From Country Currency list
Properties :

ID="ddlFromCurrency"
DataSourceID="countries" 
DataTextField="countrycode"
DataValueField="Id"
and another Dropdownlist for to bind To Country Currency list
Properties :
ID="ddlToCurrency"
DataSourceID="countries" 
DataTextField="countrycode"
DataValueField="Id"


2Textboxes - one textbox to accept currency value and another one to display result One button to get result in click event.

Next,We need xml file which contains countries list example countries.xml.

Next,We need to bind this countries.xml file to dropdownlists 


Now coming to main part of application logic.Open the Default.aspx.cs 

In button click Event create one method Ex : getValue(); 

Next,Write the following logic in getvalue method

 //Get live currency values
        try
        {
            string Result = null;
            string url;
            double CurrencyAmount = Convert.ToDouble(txtValue.Text);
            
            url = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=" + ddlFromCurrency.SelectedValue + "&ToCurrency=" + ddlToCurrency.SelectedValue + "";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader Stream = new StreamReader(response.GetResponseStream());
            XmlDocument doc = new XmlDocument();
            Result = Stream.ReadToEnd();
            doc.LoadXml(Result);
            
            string CR = doc.GetElementsByTagName("double").Item(0).InnerText;
            
            double ConvR = double.Parse(CR);

            
            double ConvAmt = CurrencyAmount * ConvR;
            
            txtresult.Text = Convert.ToString(ConvAmt);
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message;
        }
        
That's it..If you have any doubts please share your comments

Labels: , , , ,