Link to home
Start Free TrialLog in
Avatar of nflowers1228
nflowers1228

asked on

Getting dynamic data from calculation to display in data grid (no database)

Hello everyone,

I'm in my last week of class. I have made quite a bit of progress getting my mortgage calculator from a vb.net stand alone to a web application using codebehind. But I'm having trouble getting my amortization table data into a data grid. Here is what I have so far. When I run it I get an error message saying that  Object reference not set to an instance of an object. and an error on Line 40:         DisplayAmortTable.DataSource = AmortTable()

The files can be downloaded at the following url mortgage3.zip:
http://www.esnips.com/webfolder/c83fcc5b-a9c1-4886-8af0-29260925df2d

Thanks!


------ mortgagecodefile.vb ------------------------------------------

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data

Public Class myCodeBehind
    Inherits Page
    'list all form elements that are used in the programming

    Protected WithEvents myLabel As Label
    Protected WithEvents MyGrid As DataGrid
    Protected WithEvents DisplayAmortTable As DataGrid
    Protected WithEvents uiMortgageAmountTextBox As TextBox
    Protected WithEvents uiDisplayPaymentLabel As TextBox
    Protected WithEvents uiMortgageTermTextBox As TextBox
    Protected WithEvents uiInterestRateTextBox As TextBox
    Protected WithEvents uiAmortTableRichTextBox As TextBox
    Protected WithEvents uiLoanTypeListBox As ListBox
    Protected WithEvents uiCalculateButton As Button

    'Global declaration of variables

    Dim MortgageTerm As Double
    Dim MortgageAmount As Double
    Dim Payment As Double
    Dim InterestRate As Double
    Sub ResetForm(ByVal sender As Object, ByVal e As System.EventArgs)
        'reset form function
        uiMortgageAmountTextBox.Text = ""
        uiMortgageTermTextBox.Text = ""
        uiDisplayPaymentLabel.Text = ""
        uiInterestRateTextBox.Text = ""
    End Sub
    'Call calculate functions based on whether there are values in the text boxes
    Sub ASPCalcuate(ByVal s As Object, ByVal e As EventArgs)

        Payment = CalculatePayment()
        uiDisplayPaymentLabel.Text = Payment.ToString("c2")
        DisplayAmortTable.DataSource = AmortTable()
        DisplayAmortTable.DataBind()

    End Sub

    Function CalculatePayment()
        'calculates the payment if there are values entered in the text boxes
        MortgageAmount = uiMortgageAmountTextBox.Text
        MortgageTerm = uiMortgageTermTextBox.Text ' * 12 'calcualtes the mortgage term based on the value entered in the Mortgage Term Field
        InterestRate = uiInterestRateTextBox.Text ' / 100) / 12

        Return (MortgageAmount * (InterestRate / 100) / 12) / (1 - Math.Pow(1 + (InterestRate / 100) / 12, -(MortgageTerm * 12)))

    End Function
    Function AmortTable()
        'sets all the values and calculates number of payments, balance, monthly interst paid, and monthly principal balance
        MortgageAmount = uiMortgageAmountTextBox.Text
        MortgageTerm = uiMortgageTermTextBox.Text ' * 12 'calcualtes the mortgage term based on the value entered in the Mortgage Term Field
        InterestRate = uiInterestRateTextBox.Text ' / 100) / 12

        'variable declarations for looping function
        Dim Balance, MonthlyInterestPayment, MonthlyPrincipalPaid, PrincipalBalance, Rate, NewBalance As Double 'balance of the mortgage after payment is subtracted...not really a true balance because it includes the interest payment.
        Dim NumberPayments = MortgageTerm * 12

        'variable declarations for the data grid
        Dim dt As DataTable = New DataTable
        Dim dr As DataRow

        NumberPayments = 1
        Balance = MortgageAmount 'original amount
        Rate = (InterestRate / 100) / 12 'annual interest rate
        Payment = (MortgageAmount * (InterestRate / 100) / 12) / (1 - Math.Pow(1 + (InterestRate / 100) / 12, -(MortgageTerm * 12)))

        'declaring the first table row with column heading for amortization table
        dt.Columns.Add(New DataColumn("Number Payments", GetType(Double)))
        dt.Columns.Add(New DataColumn("Interest Payment", GetType(Double)))
        dt.Columns.Add(New DataColumn("Balance", GetType(Double)))

        'start of the do loop
        Do
            MonthlyInterestPayment = Balance * Rate 'calculate monthly interest rate
            MonthlyPrincipalPaid = Payment - MonthlyInterestPayment 'calculate the principle paid for that month
            '
            'new balance subtracting the amount of the monthly principal paid from the new balance
            NewBalance = Balance - MonthlyPrincipalPaid 'declares new balance after last payment was subtracted from running balance

            'declaring the first dynamic row of the table
            dr = dt.NewRow()
            dr(0) = NumberPayments
            dr(1) = MonthlyInterestPayment
            dr(2) = NewBalance

            Balance = NewBalance        'new balance
            NumberPayments = NumberPayments + 1

            'declare next row
            dt.Rows.Add(dr)

        Loop Until NumberPayments = MortgageTerm * 12

        'create data view and display table (I hope!)
        Dim dv As New DataView(dt)
        Return dv

    End Function

End Class

-----mortgage.aspx file -------------------------------

<%@ page Language="vb" inherits="myCodeBehind" src="mortgagecodefile.vb" %>
<%@ Import Namespace="System.Data" %>
<html>
<head><title>Nancy's Mortgage Calculator</title></head>
<script language='JavaScript'>
<!--
function SetFocus()
{
    document.MortgageCalculatorForm['uiMortgageAmountTextBox'].focus();
}
window.onload = SetFocus;
// -->
</script>

<body>

<H3>Nancy's Mortgage Calculator</H3>
<form Runat="Server" ID="MortgageCalculatorForm" action="mortgage.aspx">

<table width="550" cellspacing="0">
      <tr>
        <td colspan="3" bgcolor="#FFFFCC">Enter the mortgage amount... </td>
    </tr>
<!--Age textbox and validation attempt which did not work -->
  <asp:TextBox id="minMortgageAmount" text="0" runat="server" visible="False" />
  <asp:TextBox id="maxMortgageAmount" text="10000000" runat="server" visible="False" />

      <tr>
            <td>Mortgage Amount:
            </td>
            <td><asp:TextBox
            ID="uiMortgageAmountTextBox"
            Runat="Server" />
            </td>
            <td>
            <!--adding validation to be sure that a number is entered in the Mortgage Amount Field -->
                  <asp:RequiredFieldValidator runat="server"
                        id="reqMortgageAmount" ControlToValidate="uiMortgageAmountTextBox"
                        ErrorMessage = "Please enter a number in this text box."
                        display="Dynamic" />
            </td>
      </tr>
      <tr>
            <td colspan="2" bgcolor="#FFFFCC">Then enter the Mortgage Term and Rate ... </td>
            <td  bgcolor="#FFFFCC">Or Choose One...
    </tr>
    <tr>
            <td>Mortgage Term:
            </td>
            <td><asp:TextBox
            ID="uiMortgageTermTextBox"
            Runat="Server" />
            </td>
            <td rowspan="4">
                  <asp:ListBox ID="uiLoanTypeListBox" RunAt="server">
                  <asp:ListItem Text="7 years @ 5.35%" RunAt="server" />
                  <asp:ListItem Text="15 years @ 5.5%" RunAt="server" />
                  <asp:ListItem Text="30 years @ 5.75%" RunAt="server" />
                  
                  </asp:ListBox>
            </td>
      </tr>
      <tr>
            <td>Interest Rate:
            </td>
            <td><asp:TextBox
            ID="uiInterestRateTextBox"
            Runat="Server" />
            </td>
      </tr>
      <tr>
        <td colspan="3">&nbsp; </td>
    </tr>
    <tr>
    <tr>
        <td colspan="3" bgcolor="#FFFFCC">Your Monthly Payment Is: </td>
    </tr>
    <tr>
            <td>Payment:
            </td>
            <td><asp:TextBox
                  ID="uiDisplayPaymentLabel"
                  Runat="Server" />
            </td>
      </tr>
      <tr>
            <td>&nbsp;
            </td>
            <td>
                  <asp:Button
                        Text="Calculate!"
                        OnClick="ASPCalcuate"
                        Runat="Server" ID="CalculateButton"/>
                  
                  <asp:Button
                        Text="Reset Form"
                        OnClick="ResetForm"
                        Runat="Server" ID="ResetButton"/>
            
            </td>
      </tr>
      <tr>
            <td colspan="3">
                  <asp:label
                  id="title"
                  text="Amortization Table"
                  runat="server">
                  </asp:label>
                  
                  <asp:datagrid
                  id="MyGrid"
                  autogeneratecolumns="True"
                  runat="server">
                  </asp:datagrid>
            </td>
            </tr>
            <tr>
            <td><asp:datagrid
                  id="AmortTable"
                  autogeneratecolumns="True"
                  runat="server">
                  </asp:datagrid>
            </td>
      </tr>
</table>
</form>

</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of nehaya
nehaya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nflowers1228
nflowers1228

ASKER

yes that was it. Thank you very much. I was making myself crazy over something so simple! Thanks again!