Cant figure out how to resovle error message "input string was not in a correct forma
Hi,
I'm debugging my application and the value from my textbox is always = "" (blank value)even after a value has been entered. This is what i think is causing this error message "input string was not in a correct format ". Can someone point me to how to fix this error why my values that i enter is not being passed to the textboxt.text and the value is always blank thanks... Is this a setting issue or something else. Thanks..
Re: Cant figure out how to resovle error message "input string was not in a correct f
Thanks Big ed for the reply here is sample of my code.. thanks..
Quick eplanation of my code. There is 6 textboxes that i pick up text from which are number text. After that I click a button that fires of my listin method. This goes to my textbox change the number string that i enter into an integer. I take those integer and add them to a list. But when the code fires off. It throws off that error "input string was not in a correct format" when i debug it. The textbox value always seems to be Null or blank
Re: Cant figure out how to resovle error message "input string was not in a correct f
dglienna
myVal = textBox1.Text;
numTxt1 = int.Parse(myVal); This is the line that is throwing the error. I debugged it.
It seems like the value coming from the textbox is Null or "" blank even after entering a value in such as 1 or 22 eg.. I don't know why its keep saying that the value in the textbox is null or blank when it should be the value that i entered into it..
Re: Cant figure out how to resovle error message "input string was not in a correct f
Does anyone know if this can be due to how my method was created or how its inside my
public partial class Form1 : Form or is this a setting issue. I've been racking my brain and searching the internet to see why the characters that are being entered are coming back "" blank ..From debugging this is what i have come up with when i put the break point inside the button click even i see the text that are in the textbox but when i leave the even to go to the Listin Method it sets my textbox back to "" I don't know why its doing that. Any help would be greatly appreciated..Thanks..
Last edited by wilnicm; September 8th, 2010 at 07:09 PM.
Is that button1_Click handler inside the Form1 class? If so why are you creating another instance of Form1 inside the button handler?
Furthermore, in the ListIn method. You pass in the string params myVal myVal2, etc. which are initialized by textBox1.Text, textBox2.Text and so on.
Since these values are already passed into the method, why are you accessing the textBoxes again (near the end of the method)?
Code:
public List<int> ListIn(string myVal, string myVal2, string myVal3, string myVal4, string myVal5, string myVal6)
{
int numTxt1, numTxt2, numTxt3, numTxt4, numTxt5, numTxt6, total;
// Convert the first number to numeric
myVal = textBox1.Text; // wrong, you're overwriting the value you've just passed in (since you've create a new Form1 instance, textBox1.Text will be blank
numTxt1 = int.Parse(myVal);
// Convert the second number to numeric
numTxt2 = int.Parse(textBox2.Text.Trim());
//// Convert the third number to numeric
numTxt3 = int.Parse(textBox3.Text.Trim());
...
}
I'll take a guess here and assume that the button1_Click handler and ListIn method is inside Form1.
Option 1) Rewrite your ListIn method to use the strings that you pass in.
Code:
public List<int> ListIn(string myVal, string myVal2, string myVal3, string myVal4, string myVal5, string myVal6)
{
int numTxt1, numTxt2, numTxt3, numTxt4, numTxt5, numTxt6, total;
// Convert the first number to numeric
numTxt1 = int.Parse( myVal );
// Convert the second number to numeric
numTxt2 = int.Parse( myVal2 );
//// Convert the third number to numeric
numTxt3 = int.Parse( myVal3 );
...
}
Option 2) Rewrite your ListIn to not pass in any strings and just get the values from the textboxes directly
Code:
public List<int> ListIn( )
{
int numTxt1, numTxt2, numTxt3, numTxt4, numTxt5, numTxt6, total;
// Convert the first number to numeric
numTxt1 = int.Parse( textBox1.Text );
// Convert the second number to numeric
numTxt2 = int.Parse( textBox2.Text );
//// Convert the third number to numeric
numTxt3 = int.Parse( textBox3.Text );
...
}
Last edited by Arjay; September 8th, 2010 at 07:56 PM.
Re: Cant figure out how to resovle error message "input string was not in a correct f
Arjay you hit it right on the head.
====since you've create a new Form1 instance, textBox1.Text will be blank ===
...
When i was first creating my method i was having some issues becuase i had my textbox in there and some labels and i made it a static void and got the error message about non-static objects having to be reference and it through off my thinking on what i was doing i thought i had to instantiate the form inside the button click to be able to access the method. But i don't know why i thought that because i can call the method with just method(); or method(arg, arg, arg etc..);
but instantiating the form again cause it to overite my textboxes.. Thanks..
Bookmarks