1

I have been trying for ages to convert a string taken from an input box in my asp form and converting it to an integer.

This integer variable will then be used for a database operation I have planned.

Here is what I have tryed:

 string mytxtCHI = txtID.Text;
            int @CHI = Convert.ToInt16(mytxtCHI);

Error:

System.FormatException {"Input string was not in a correct format."}

Things I have tried:

-  string mytxtCHI = txtID.Text;
            int myNewID=(Int32.Parse(txtID.Text));

Thankyou for your time :)

5
  • 2
    looks like you don't have a number in your input box. What is the text? Commented Jul 15, 2011 at 20:54
  • How does your input looks like Commented Jul 15, 2011 at 20:55
  • 1
    What value are you putting in your textbox? A simple Parse() should work, although the answers below suggesting TryParse() are a lot more robust. Commented Jul 15, 2011 at 20:57
  • 1
    The moral of all the answer(s), always TryParse since you never know what the users going to give you in a textbox... Commented Jul 15, 2011 at 20:57
  • you are right, I ran RUN-TO-CURSOR and for some reason the textbox control is not picking the entry I make. However, when I hard code the value to a variable, the textbox picks it. See below: string test2 = "running nuts"; txtID.Text = test2; Commented Jul 15, 2011 at 20:58

5 Answers 5

1

Your way is correct, but what you should do to avoid errors

int myNewID = 0;

    if(int.TryParse(txtID.Text, out myNewID))
     {
        //your code here
    }
    else
    {
       //your error handling here
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks developers, TryParse Worked
1

You should use tryparse:

  string mytxtCHI = txtID.Text;
  int number;
  bool result = Int32.TryParse(mytxtCHI , out number);
  if (result)
  {
     Console.WriteLine("Converted '{0}' to {1}.", mytxtCHI , number);         
  }
  else
  {
     if (value == null) value = ""; 
     Console.WriteLine("Attempted conversion of '{0}' failed.", mytxtCHI );
  }

Comments

1

Put a breakpoint on the line in Visual Studio. In debug mode, run to the breakpoint, and hover over txtID.Text to see what's in it.

When in the page lifecycle are you checking this value? Check it after Page Init. Check HttpRequest.Form to see what came down the wire.

Comments

0

You should use Int.TryParse, and handle the case when the value isn't a valid integer.

string mytxtCHI = txtId.Text;
int val;
if (!int.TryParse(mytxtCHI, out val))
{
    // error here, value was not a valid integer.
}
// here, val contains the value, expressed as an integer.

Comments

0

Use watch window or System.Diagnostics.Debug.WriteLine(mytxtCHI) to check what's the value you are passing, is it blank?

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.