You must Sign In to post a response.
  • Category: .NET

    Index and length must refer to a location within the string. Parameter name: length

    i get a error.. let me knw how to solve it

    Index and length must refer to a location within the string.
    Parameter name: length
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
    Parameter name: length

    Source Error:


    Line 131:
    Line 132:
    Line 133: if (dealwith.Substring(0,8 ).ToString() == "|")
    Line 134: dealwith = dealwith.Substring(1, dealwith.Length - 1);
  • #769792
    What is the second parameter in your Substring()? It should be the length of sub string. Often we are mistaken considering it as the end index. So it should be the length of sub string and check whether it exceeds the length of main string.

  • #769793
    Hi Partiban,

    First thing:
    Is your dealwith string has more than 8 characters ? because you are trying to extract substring which of 8 characters from the main string ie dealwith. If the main string dealwith is not having enough characters then your substring condition will fail

    Second thing: If condition itself wrong, you are trying to extract 8 characters and comparing that 8-character string with single pipe ("|") character which will never be matched.

    You can try like this if you want to really check for "|"

    if(dealwith.Contains("|"))
    {
    //Your Logic
    }


  • Sign In to post your comments