1

I am trying to convert an inputted string to an int. I have tried int.parse, and int.parse32 but when I press "enter" I get the following error:

System.FormatException: Input string was not in a correct format.
  at System.Number.StringToNumber(String str, NumberStyles options, 
                                  NumberBuffer & number...."

partial class Form1:

this.orderID.Text = currentID;
this.orderID.KeyPress += new KeyPressEventHandler(EnterKey);

partial class Form1:Form:

  public int newCurrentID;
  private void EnterKey(object o, KeyPressEventArgs e)
    {
        if(e.KeyChar == (char)Keys.Enter)
        {
            try
            {
                newCurrentID = int.Parse(currentID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            e.Handled = true;
        }
    }
7
  • 8
    Put a break point to method EnterKey and see what currentID contains. Commented May 14, 2013 at 7:11
  • 2
    What type is currentID and what is its content? Commented May 14, 2013 at 7:12
  • what do u find inside currentID just when u parse it Commented May 14, 2013 at 7:14
  • 2
    Are you relying on the fact that you set currentID at the top of the class? This wont pick up changes to currentID and will/may be empty when the form loads. Try getting the value from the control directly; for example, Int.TryParse(orderID.Text, out someInt); Commented May 14, 2013 at 7:14
  • 2
    You don't show code where the value of orderID.Text is copied into currentID. You do have that, don't you? Commented May 14, 2013 at 7:16

4 Answers 4

4

String is immutable so when you assign currentID to the textbox any changes of that text will not be reflected in the variable currentID

this.orderID.Text = currentID;

What you need to do in the EnterKey function is to use the Textbox value directly:

private void EnterKey(object o, KeyPressEventArgs e)
{
        if(e.KeyChar == (char)Keys.Enter)
        { 
            if(!int.TryParse(orderID.Text, out newCurrentID))
               MessageBox.Show("Not a number");
            e.Handled = true;
        }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helped me out alot, and taught me a new thing as well. :)
4

Check string for string.IsNullOrEmpty() and do not try to parse such strings.

1 Comment

I'd like to mention the newer string.IsNullOrWhitespace() method (since C# 4.0), does some extra checking for the user :).
1

Use TryParse instead of parse the value directly:

int intResult = 0;

if (Int32.TryParse(yourString, out intResult) == true)
{
    // do whatever you want...
}

Comments

0

Try This Code

if (!string.IsNullOrEmpty(currentID)){
     newCurrentID = int.Parse(currentID);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.