Simple steps to create Currency converter application using Asp.net and c#
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"
Next,We need xml file which contains countries list example countries.xml.
Next,We need to bind this countries.xml file to dropdownlists
check this tutorial http://www.developerscode.com/2011/05/add-xml-datasource-to-gridview-dropdown.html.
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: .Net Tutorials, Ajax, Aspnet, C#, Xml

<< Home