4 Replies - 1251 Views - Last Post: 10 October 2012 - 09:32 AM Rate Topic: -----

#1 wolne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 28-June 12

Input string was not in the correct format

Posted 09 October 2012 - 08:52 PM

I keep getting an error when I run this form saying input string was not in the correct format.
I know the error is in the line
//if (!Double.TryParse(Deposit_txtBox.Text, out DepositAmount))
// {
DepositAmount = double.Parse(Deposit_txtBox.Text);

but Im not sure what to do to fix it.

       private void Deposit_button_Click(object sender, EventArgs e)
        {
            if (Deposit_txtBox.Text == "")
            {
                MessageBox.Show("Please enter the amount you want to deposit.");
                return;
            }
            
            double AccountBalance;
            double NewAccountBalance;
            AccountBalance = 500;
            double DepositAmount ;

        
            //if (!Double.TryParse(Deposit_txtBox.Text, out DepositAmount))
           // {
                DepositAmount = double.Parse(Deposit_txtBox.Text);
                NewAccountBalance = AccountBalance + DepositAmount;
                Deposit_button.Visible = false;
                lblDeposit.Visible = true;
                Deposit_txtBox.Visible = true;
                lblOldBalance.Visible = true;
                lblOldBalanceAmount.Text = "$ " + AccountBalance.ToString();
                lblDepositAmount.Visible = true;
                lblDepositAmount.Text = "$ " + DepositAmount.ToString();
                lblNewBalance.Visible = true;
                lblNewBalanceAmount.Text = "$ " + (AccountBalance + DepositAmount);
           // }


        }



Is This A Good Question/Topic? 0
  • +

Replies To: Input string was not in the correct format

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Input string was not in the correct format

Posted 09 October 2012 - 08:59 PM

What did you put in the textbox? What culture is the machine running under?
Was This Post Helpful? 0
  • +
  • -

#3 wolne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 28-June 12

Re: Input string was not in the correct format

Posted 09 October 2012 - 09:07 PM

View PostMomerath, on 09 October 2012 - 08:59 PM, said:

What did you put in the textbox? What culture is the machine running under?



I just realized what the problem was.. I had directions in the text box for the user. I had to delete that and then my code worked perfectly.
Was This Post Helpful? 0
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: Input string was not in the correct format

Posted 10 October 2012 - 08:43 AM

You should bring the TryParse back, too. That should have been your first indicator of the problem (if it's failing, you're not passing it a number). You had the logic backwards, though. What you want to say is if (Double.TryParse(Deposit_txtBox.Text, out DepositAmount)).... If it returns true, then perform the actions on DepositAmount. Add an else that notifies the user that their input was bad, because if it falls into the else clause, the TryParse failed.
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Input string was not in the correct format

Posted 10 October 2012 - 09:32 AM

You are also essentially doing the parsing twice. Just the .TryParse is needed. You get the result in DepositAmount through the out parameter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1