I?m not sure if anyone can help, but I'm trying to use a feature covered in the SAMS Publishing ?ASP.NET 2.0 in 24 Hours? book.
I?m trying to use the Financial Calculator outlined in the book for a website I?m building. I have got the calculator to work by following the steps in the book. Here's the code for it.
FinancialCalculator.aspx
1 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="FinancialCalculator.aspx.vb" Inherits="FinancialCalculator" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml" >
6 <head runat="server">
7 <title>Untitled Page</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 Amount: <asp:TextBox ID="loanAmount" runat="server"></asp:TextBox><br />
13 Interest Rate:
14 <asp:TextBox ID="rate" runat="server"></asp:TextBox><br />
15 Term:
16 <asp:TextBox ID="mortgageLength" runat="server"></asp:TextBox><br />
17 <br />
18 <asp:Button ID="performCalc" runat="server" Text="Compute Monthly Cost" /><br />
19 <br />
20 <asp:Label ID="results" runat="server"></asp:Label></div>
21 </form>
22 </body>
23 </html>
FinancialCalculator.aspx.vb
1
2 Partial Class FinancialCalculator
3 Inherits System.Web.UI.Page
4
5 Protected Sub performCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles performCalc.Click
6 Const INTEREST_CALCS_PER_YEAR As Integer = 12
7 Const PAYMENTS_PER_YEAR As Integer = 12
8
9 Dim P As Double = loanAmount.Text
10 Dim r As Double = rate.Text / 100
11 Dim t As Double = mortgageLength.Text
12
13 Dim ratePerPeriod As Double
14 ratePerPeriod = r / INTEREST_CALCS_PER_YEAR
15
16 Dim payPeriods As Integer
17 payPeriods = t * PAYMENTS_PER_YEAR
18
19 Dim annualRate As Double
20 annualRate = Math.Exp(INTEREST_CALCS_PER_YEAR * Math.Log(1 + ratePerPeriod)) - 1
21
22 Dim intPerPayment As Double
23 intPerPayment = (Math.Exp(Math.Log(annualRate + 1) / payPeriods) - 1) * payPeriods
24
25 Dim intPerMonth As Double = intPerPayment / PAYMENTS_PER_YEAR
26
27 Dim costPerMonth As Double
28 costPerMonth = P * intPerMonth / (1 - Math.Pow(intPerMonth + 1, -payPeriods))
29
30 results.Text = "Your monthly repayments will be ?" & costPerMonth
31
32 End Sub
33 End Class
34
But when trying to use it in the website I?m building, it won?t work. This website I?m building uses a master page.
Here is my code for these pages.
MasterPage.master
1 <%@ Master Language="VB" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <script runat="server">
6
7 </script>
8
9 <html xmlns="http://www.w3.org/1999/xhtml" >
10 <head id="Head1" runat="server">
11 <title>The Gateway Calculators Suite</title>
12 <link rel="stylesheet" type="text/css" href="styles_generic.css" />
13 </head>
14 <body>
15 <form id="form1" runat="server">
16 <!-- START HEADER -->
17 <div id="header">
18 </div>
19 <!-- END HEADER -->
20
21 <!-- CONTENT CONTAINER -->
22 <div id="wrapper">
23
24 <!-- NAVIGATION -->
25 <div class="leftMenu">
26 <h2>Welcome</h2>
27 <a class="menuLeft" href="Default.aspx">Calculators Home</a>
28 <a class="menuLeft" href="http://www.gatewayconsortium.com" target="_blank">Gateway Portal</a>
29 <h2>Calculators</h2>
30 <a class="menuLeft" href="Overture.aspx">Overture</a>
31 <a class="menuLeft" href="Foundation.aspx">Foundation</a>
32 <a class="menuLeft" href="DirRemun.aspx">Directors Remuneration</a>
33 <a class="menuLeft" href="ComLoans.aspx">Commercial Loans</a>
34 <a class="menuLeft" href="ComProp.aspx">Commercial Property</a>
35 <a class="menuLeft" href="Graph.aspx">Graph Generator</a>
36 <a class="menuLeft" href="Compound.aspx">Compound Interest</a>
37 </div>
38 <!-- END NAVIGATION -->
39
40 <!-- CONTENT -->
41 <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
42
43 </asp:contentplaceholder>
44
45 </div>
46 <!-- END CONTAINER -->
47
48 <!-- FOOTER -->
49 <div id="footer">
50 <p style="display:inline;float:left;"> ?Copyright 2007 - The Gateway Consortium Limited - All rights reserved</p>
51 <p style="display:inline;float:right;margin-right:5px;">Developed by Steve Williams</p>
52 </div>
53 <!-- END FOOTER -->
54 </form>
55 </body>
56 </html>
57
ComLoans.aspx
1 <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="ComLoans.aspx.vb" Inherits="ComLoans" title="The Gateway Calculators - Commercial Loans" %>
2 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
3 <div class="content">
4 <h1>Commercial Loans</h1>
5 <table>
6 <tr>
7 <td>Amount to borrow:</td>
8 <td style="width:20px;text-align: right;">?</td>
9 <td><asp:TextBox ID="txtAmount" runat="server" BackColor="#FFF6C3" BorderColor="Navy" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox></td>
10 </tr>
11 <tr>
12 <td>Interest Rate:</td>
13 <td style="width:20px;text-align: right;"> </td>
14 <td><asp:TextBox ID="txtRate" runat="server" BackColor="#FFF6C3" BorderColor="Navy" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>%</td>
15 </tr>
16 <tr>
17 <td>Term:</td>
18 <td style="width:20px;text-align: right;"> </td>
19 <td><asp:TextBox ID="txtTerm" runat="server" BackColor="#FFF6C3" BorderColor="Navy" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox> years</td>
20 </tr>
21 <tr><td colspan="3"> </td></tr>
22 <tr>
23 <td colspan="3">
24 <asp:Button ID="btnCalc" runat="server" Text="Calculate Monthly Repayments" />
25 <input id="Reset" type="reset" value="Reset Form" />
26 </td>
27 </tr>
28 <tr><td colspan="3"> </td></tr>
29 <tr>
30 <td>Monthly Cost:</td>
31 <td style="width:20px;text-align: right;">?</td>
32 <td><asp:Label ID="lblRepay" runat="server" Text=""></asp:Label>
33 </tr>
34 </table>
35
36 </div>
37 </asp:Content>
38
39
ComLoans.aspx.vb
1
2 Partial Class ComLoans
3 Inherits System.Web.UI.Page
4
5 Protected Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click
6 'Specify constant values
7 Const INTEREST_CALCS_PER_YEAR As Integer = 12
8 Const PAYMENTS_PER_YEAR As Integer = 12
9
10 'Create variable to hold the values entered in text boxes
11 Dim P As Double = txtAmount.Text
12 Dim r As Double = txtRate.Text / 100
13 Dim t As Double = txtTerm.Text
14
15 Dim ratePerPeriod As Double
16 ratePerPeriod = r / INTEREST_CALCS_PER_YEAR
17
18 Dim payPeriods As Integer
19 payPeriods = t * PAYMENTS_PER_YEAR
20
21 Dim annualRate As Double
22 annualRate = Math.Exp(INTEREST_CALCS_PER_YEAR * Math.Log(1 + ratePerPeriod)) - 1
23
24 Dim intPerPayment As Double
25 intPerPayment = (Math.Exp(Math.Log(annualRate + 1) / payPeriods) - 1) * payPeriods
26
27 'Work out total cost of the loan
28 Dim intPerMonth As Double = intPerPayment / PAYMENTS_PER_YEAR
29
30 Dim costPerMonth As Double
31 costPerMonth = P * intPerMonth / (1 - Math.Pow(intPerMonth + 1, -payPeriods))
32
33 'Display result in the label control
34 lblRepay = costPerMonth
35
36 End Sub
37 End Class
38
The error I am getting is:
Compiler Error Message: BC30311: Value of type 'Double' cannot be converted to 'System.Web.UI.WebControls.Label'.
.. which is related to me trying to display the result in a label web control when the user submits it. I don't understand why this is happening upon loading the page, because this event shouldn't occur until the user clicks the button.
I am using Microsoft's Visual Web Developer Express 2005 to build this site.
I have attached the code rather than post it in code boxes here because there is a lot of it.