Results 1 to 5 of 5
  1. #1
    Senior Member
    superprada is offline

    Posts
    121

    StartIndex cannot be less than zero. Parameter name: startIndex

    Wpf application:

    quantity is a string from a parsed XML file that I downloaded by connecting to gmail through gmail imap

    textBox_Quantity.Text = quantity; (actual text would be a number followed by return, e.g "1\r\n")
    string quantityText = textBox_Quantity.Text.Remove(textBox_Quantity.Text .IndexOf("\r\n"), 2);
    int quantityInt = System.Convert.ToInt32(quantityText);

    I need to remove the \r\n so I can convert string to int

    error when debugging:
    StartIndex cannot be less than zero.
    Parameter name: startIndex

    If I comment out the second and third lines the result is "1\r\n"

    Why?

    Also, how do I close an Excel workbook that I opened in c#, because every time I stop debugging the EXCEL.EXE process doesn't stop running.
    Last edited by superprada; 07-21-2013 at 06:01 PM.

  2. #2
    Server Admin
    Sticky is offline

    Posts
    964
    It appears that IndexOf is returning -1 (which means it couldn't find "\r\n").

  3. #3
    Senior Member
    superprada is offline

    Posts
    121
    Yes, I know, but I don't know why it's finding the index of \r\n to be -1, it's definitely there...
    I just checked the input from textBox_Quantity.Text, and it's "". However, as I said when I manually check textBox_Quantity.Text it shows "1\r\n"

  4. #4
    Bot Coder
    flashbang is offline

    Posts
    584
    I didn't work with C# in ages but from other languages at least \r\n are control chars so you need to use ' instead of " else it gets misinterpreted. Also check the encoding of your .xml file. (I work with Linux mostly so not sure which are which on Windows but I often have this problem with files created on Windows and then being used on Linux) This is a bit tricky but fiddle around a bit with it and you will eventually work it out.

    Alternatively and less neat, you can iterate through the string and check if it is an int char or not and break on mismatch.

    //siggy by Poseidon

  5. #5
    Member
    kyborek is offline

    Posts
    57
    Hello, my guess would be that the "quantity" is sometimes empty string. Which means it sometimes gives you the value 1\r\n and sometimes it crashes with the error.
    I have found a non-crashing solution in this question:
    What is the easiest way in C# to trim a newline off of a string? - Stack Overflow
    The code you might need is:
    textBox_Quantity.Text = quantity;
    int quantityInt = System.Convert.ToInt32(quantity.TrimEnd( Environment.NewLine.ToCharArray()));

    If this still does not work, try sorrounding the code with try & catch:
    try {
    textBox_Quantity.Text = quantity; (actual text would be a number followed by return, e.g "1\r\n")
    string quantityText = textBox_Quantity.Text.Remove(textBox_Quantity.Text .IndexOf("\r\n"), 2);
    int quantityInt = System.Convert.ToInt32(quantityText);
    }
    catch (Exception ex)
    {
    MessageBox.Show("Quantity is ["+quantity"]");
    }


    And regarding the workbook problem, i guess it has some .Close() method.

Posting Permissions

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