DataTable insert error

c00012 746 Reputation points
2022-02-17T12:39:44.367+00:00

Hello,

I wrote a code to show amortization schedule on the screen. so I made UI like this:

175368-ice-screenshot-20220217-211414.png

when you press "Estimate" button on the screen above, you can see the result on the below screen:

175369-ice-screenshot-20220217-211520.png

These are the properties to use to calculate schesule:

//properties   
        private string _pllender;//lender  
        public string PLLender  
        {  
            get => _pllender;  
            set => SetProperty(ref _pllender, value);  
        }  
  
        private string _loanamt;//amount  
        public string LoanAmt  
        {  
            get => _loanamt;  
            set => SetProperty(ref _loanamt, value);  
        }  
  
        private string _plapr;//APR  
        public string PLAPR  
        {  
            get => _plapr;  
            set => SetProperty(ref _plapr, value);  
        }  
  
        private string _plterms;//Terms  
        public string PLTerms  
        {  
            get => _plterms;  
            set => SetProperty(ref _plterms, value);  
        }  
  
        private double _interest;  
        public double Interest  
        {  
            get => _interest;  
            set => SetProperty(ref _interest, value);  
        }  
  
        private double _principal;  
        public double Principal  
        {  
            get => _principal;  
            set => SetProperty(ref _principal, value);  
        }  
  
        private double _balance;  
        public double Balance  
        {  
            get => _balance;  
            set => SetProperty(ref _balance, value);  
        }  
  
        private DataTable amortable;  
        public DataTable Amortable  
        {  
            get => amortable;  
            set => SetProperty(ref amortable, value);  
        }  

This is the code is bound to "estimate" button;

var PLfactor = Convert.ToDouble(PLAPR) / 100 / 12;  
            var PLfogr = Math.Pow(1 + PLfactor, Convert.ToInt32(PLTerms) * 12);  
            var PLPayment = Convert.ToDouble(LoanAmt) * PLfactor * (PLfogr / (PLfogr - 1));  
            if (Convert.ToDouble(PLAPR) == 0)  
            {  
                PLPayment = Convert.ToDouble(LoanAmt) / (Convert.ToInt32(PLTerms) * 12);  
            }  
            var PLresult = MessageBox.Show(string.Format($" Lender:{PLLender}, Amount: ${Convert.ToDouble(LoanAmt):N0}, APR: {PLAPR}%, Terms: {Convert.ToInt32(PLTerms) * 12} months,  Payment: $ {PLPayment:N0} "), "Review your Offer", MessageBoxButton.OKCancel);  
             if(PLresult==MessageBoxResult.OK)  
            {  
                Amortable = new DataTable();  
                Amortable.Columns.Add("Terms", typeof(int));  
                Amortable.Columns.Add("Payment", typeof(double));  
                Amortable.Columns.Add("Principal", typeof(double));  
                Amortable.Columns.Add("Interest", typeof(double));  
                Amortable.Columns.Add("Balance", typeof(double));  
  
                DataRow row = Amortable.NewRow();  
                row["Balance"] = Math.Round(Convert.ToDouble(LoanAmt), 4);  
                row["Terms"] = 0;  
                row["Payment"] = 0.0;  
                row["Interest"] = 0.0;  
                row["Principal"] = 0.0;  
                Amortable.Rows.InsertAt(row, 0);  
  
                for (int i = 0; i < Convert.ToInt32(PLTerms) * 12; i++)  
                {  
                    DataRow dataRow = Amortable.NewRow();  
                    dataRow["Terms"] = i + 1;  
                    dataRow["Payment"] = Math.Round(Convert.ToDouble(PLPayment), 4);  
                    dataRow["Interest"] = Math.Round(Convert.ToDouble(PLAPR) / 12 / 100 * Convert.ToDouble(Amortable.Rows[i].Field<double>("Balance")), 4);  
                    dataRow["Principal"] = Math.Round(Convert.ToDouble(dataRow["Payment"]) - Convert.ToDouble(dataRow["Interest"]), 4);  
                    dataRow["Balance"] = Math.Round(Convert.ToDouble(Amortable.Rows[i].Field<double>("Balance")) - Convert.ToDouble(dataRow["Principal"]), 4);  
                    if (Convert.ToDouble(dataRow["Balance"]) < 0.0)  
                        dataRow["Balance"] = 0.0;  
                    Amortable.Rows.Add(dataRow);  
                }  
                //add row covers total sum  
                DataRow sumRow = Amortable.NewRow();  
                sumRow["Payment"] = Amortable.Compute("SUM(Payment)", string.Empty);  
                sumRow["Interest"] = Amortable.Compute("SUM(Interest)", string.Empty);  
                sumRow["Principal"] = Amortable.Compute("SUM(Principal)", string.Empty);  
                Amortable.Rows.Add(sumRow);  
            }  
            Estimation est = new();  
            est.Show();  
        }  
  

I didn't find any error after running the code. but there's nothing on the result screen. I can't figure out what's wrong.

If someone let me know what the error is, I'd be very appreciated.

thanks,

c00012

Developer technologies | Windows Presentation Foundation
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.