CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2010
    Posts
    26

    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..

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Cant figure out how to resovle error message "input string was not in a correct f

    TextBox.Text works, the problem is somewhere in your code. Show us the relevant code sample please.

  3. #3
    Join Date
    Aug 2010
    Posts
    26

    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


    private void button1_Click(object sender, EventArgs e)
    {
    List<int> fromListIn;
    Form1 frm = new Form1();
    fromListIn = frm.ListIn(textBox1.Text.Trim(), textBox2.Text.Trim(),textBox3.Text.Trim(),textBox4.Text.Trim(),textBox5.Text.Trim(),textBox6.Text.Trim());





    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;
    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());

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Cant figure out how to resovle error message "input string was not in a correct f

    Please use CODE TAGS. Also, which line is causing the error? Put a breakpoint right before it, and see what is in each field.

    Code:
    ' Like this;
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Aug 2010
    Posts
    26

    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..

  6. #6
    Join Date
    Aug 2010
    Posts
    26

    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.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cant figure out how to resovle error message "input string was not in a correct f

    Zip up the whole project and attached it here.

    It's hard to tell what's going on without seeing more of the code.

    For example. You are in some button handler with...
    Code:
    private void button1_Click(object sender, EventArgs e)
    {
      List<int> fromListIn;
      Form1 frm = new Form1(); // Probably not needed
      fromListIn = frm.ListIn(
        textBox1.Text.Trim()
        , textBox2.Text.Trim()
        , textBox3.Text.Trim()
        , textBox4.Text.Trim()
        , textBox5.Text.Trim()
        , textBox6.Text.Trim() );
    }
    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.

    Rewrite the button handler as follows
    Code:
    private void button1_Click(object sender, EventArgs e)
    {
      List<int> fromListIn = ListIn(
        textBox1.Text.Trim()
        , textBox2.Text.Trim()
        , textBox3.Text.Trim()
        , textBox4.Text.Trim()
        , textBox5.Text.Trim()
        , textBox6.Text.Trim() );
    }
    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.

  8. #8
    Join Date
    Aug 2010
    Posts
    26

    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..

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cant figure out how to resovle error message "input string was not in a correct f

    Great. Glad to see you stepping through the code and reading values with the debugger.

    Keep in mind what I mentioned about passing string values into the ListIn method and reading the textbox.Text inside the same method.

    If you are going to pass in the values, then you don't need to read the values from the textbox.

    Or don't pass in the values and just get them from the textboxes.

    You don't need to do both.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured