Input string was not in a correct format

Anonymous
2022-06-11T19:17:06.787+00:00

Hey All!

When the "Loan Amount" textbox is left empty in my form [seen below] .NET throws an exception [also listed below].

Any help is appreciated! [I put my code below as well] I looked at some of the other postings related to the issue and still couldn't figure it out which is why I decided to post.

210468-net-exception.png
210513-web-page.png

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
  
namespace CIS2222_Lab2  
{  
    public partial class InterestCalculator : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
  
        }  
  
        protected void BtnCalculateInterest_Click(object sender, EventArgs e)  
        {  
            double calculatedInterest;  
            double loanAmt = Convert.ToDouble(txtLoanAmt.Text);  
            double interestRate = (Convert.ToDouble(radioInterestRate.Text)) / 100;  
            int numOfYears = Convert.ToInt32(listNumOfYears.Text);  
  
            calculatedInterest = loanAmt * interestRate * numOfYears;  
  
            lblResult.Text = String.Format("Total interest for a loan of {0:c} at {1:0%} interest for {2} years is {3:c}",  
                            loanAmt, interestRate, numOfYears, calculatedInterest);  
        }  
Developer technologies .NET .NET Runtime
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-06-11T21:53:43.787+00:00

    Either use Data Annotations for validating prior to using or old school assertion e.g.

    if (double.TryParse(txtLoanAmt.Text, out var loanAmt) &&   
        double.TryParse(radioInterestRate.Text, out var interestRate) &&   
        int.TryParse(listNumOfYears.Text, out var numOfYears)) {  
        // do work  
        var calculatedInterest = loanAmt * interestRate * numOfYears;  
    }  
    else  
    {  
        // have invalid value(s)  
    }  
    

    Or double assertion

    if (  
        !string.IsNullOrWhiteSpace(txtLoanAmt.Text) &&   
        double.TryParse(txtLoanAmt.Text, out var loanAmt))  
    {  
          
    }  
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2022-06-11T23:15:22.277+00:00

    Thanks Karen.

    Based on what you said, I made one little tweak to the code in line 20, which seemed to make it work [tweaked code below].

    What do you think?

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
      
    namespace CIS2222_Lab2  
    {  
        public partial class InterestCalculator : System.Web.UI.Page  
        {  
            protected void Page_Load(object sender, EventArgs e)  
            {  
      
            }  
      
            protected void BtnCalculateInterest_Click(object sender, EventArgs e)  
            {  
                double calculatedInterest;  
                **double.TryParse(txtLoanAmt.Text, out double loanAmt);**  
                double interestRate = (Convert.ToDouble(radioInterestRate.Text)) / 100;  
                int numOfYears = Convert.ToInt32(listNumOfYears.Text);  
      
                calculatedInterest = loanAmt * interestRate * numOfYears;  
      
                lblResult.Text = String.Format("Total interest for a loan of {0:c} at {1:0%} interest for {2} years is {3:c}",  
                                loanAmt, interestRate, numOfYears, calculatedInterest);  
            }  
    
    0 comments No comments

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.