I have problem in webpage , Visual Studio claims that problem is with this line decimal newAmount = PLNamount * Decimal.Parse(item.Value);
. The solution is crushed when I choose the Current (web page is simple current converter) .
this is listing of CurrencyConverter.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class CurrencyConverter : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
// The HtmlSelect control accepts text or ListItem objects.
Currency.Items.Add(new ListItem("Euros", "0.25"));
Currency.Items.Add(new ListItem("US Dollar", "0.32"));
Currency.Items.Add(new ListItem("British Pound", "0.205"));
}
}
protected void Convert_ServerClick(object sender, EventArgs e)
{
decimal PLNamount;
bool success = Decimal.TryParse(PLN.Value,out PLNamount);
if (success)
{
PLNamount = Decimal.Parse(PLN.Value);
ListItem item = Currency.Items[Currency.SelectedIndex];
decimal newAmount = PLNamount * Decimal.Parse(item.Value); //prollematic line
Result.InnerText = PLNamount.ToString() + " Polish PLN = ";
Result.InnerText += newAmount.ToString() + " " + item.Text;
}
else Result.InnerText = "Invalid content";
}
}
This is listing of CurrencyConverter.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Currency Converter</title>
</head>
<body>
Simple Currency Converter in ASP.NET web forms <br />
<form id="Form1" runat="server">
<div>
Convert:
<input type="text" ID="PLN" runat="server" />
Polish PLN to Euros.
<select ID="Currency" runat="server" />
<br /><br />
<input type="submit" value="OK" ID="Convert" runat="server"
OnServerClick="Convert_ServerClick" />
<br /><br />
<div style="font-weight: bold" ID="Result" runat="server"></div>
</div>
</form>
</body>
</html>
This Code is changed listing from apress "Pro ASP.NET 3.5 in C# 2008, Second Edition" (original version) from chapter 5. Link to source code apress.com/book/downloadfile/3803 . I have exactly the same problem in the same line
Original source code from the book
*aspx
Currency Converter
<div style="border-right: thin ridge; padding-right: 20px; border-top: thin ridge;
padding-left: 20px; padding-bottom: 20px; border-left: thin ridge; width: 531px;
padding-top: 20px; border-bottom: thin ridge; font-family: Verdana; background-color: #FFFFE8">
Convert:
<input type="text" ID="US" runat="server" style="width: 102px" /> U.S. dollars to
<select ID="Currency" runat="server" />
<br /><br />
<input type="submit" value="OK" ID="Convert" runat="server" OnServerClick="Convert_ServerClick" />
<input type="submit" value="Show Graph" ID="ShowGraph" runat="server" OnServerClick="ShowGraph_ServerClick" />
<br /><br />
<img ID="Graph" alt="Currency Graph" scr="" runat="server" />
<br /><br />
<div style="font-weight: bold" ID="Result" runat="server"></div>
</div>
</form>
*aspx.cs
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
public partial class CurrencyConverter : System.Web.UI.Page { protected void Page_Load(Object sender, EventArgs e) { if (this.IsPostBack == false) { // The HtmlSelect control accepts text or ListItem objects. Currency.Items.Add(new ListItem("Euros", "0.85")); Currency.Items.Add(new ListItem("Japanese Yen", "110.33")); Currency.Items.Add(new ListItem("Canadian Dollars", "1.2")); } Graph.Visible = false; } protected void Convert_ServerClick(object sender, EventArgs e) { decimal amount; bool success = Decimal.TryParse(US.Value, out amount); if (success) { // Retrieve the selected ListItem object by its index number. ListItem item = Currency.Items[Currency.SelectedIndex];
decimal newAmount = amount * Decimal.Parse(item.Value);
Result.InnerText = amount.ToString() + " U.S. dollars = ";
Result.InnerText += newAmount.ToString() + " " + item.Text;
}
else
{
Result.InnerText = "The number you typed in was not in the correct format. ";
Result.InnerText += "Use only numbers.";
}
}
protected void ShowGraph_ServerClick(object sender, EventArgs e)
{
Graph.Src = "Pic" + Currency.SelectedIndex.ToString() + ".png";
Graph.Visible = true;
}
}
Problem solved
This is link to print screen with this error. http://img210.imageshack.us/f/currencyerror.png/