Length cannot be less than zero. Parameter name: length


  • I am getting this error "Length cannot be less than Zero".

    After this error occured the application stops working and this same error messages gets displayed in all the pages.

    If i replace the dll files again then the application starts working again.

    I am pasting the complete error message below


    Length cannot be less than zero. 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: Length cannot be less than zero. Parameter name: length

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [ArgumentOutOfRangeException: Length cannot be less than zero. Parameter name: length] System.String.Substring(Int32 startIndex, Int32 length) +172 System.Web.UI.Util.GetScriptLocation(HttpContext context) +202 System.Web.UI.WebControls.BaseValidator.RegisterValidatorCommonScript() +69 System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +44 System.Web.UI.Control.PreRenderRecursiveInternal() +62 System.Web.UI.Control.PreRenderRecursiveInternal() +125 System.Web.UI.Control.PreRenderRecursiveInternal() +125 System.Web.UI.Page.ProcessRequestMain() +1499 

    please help me if someone know how to fix this error.

    Monday, October 17, 2005 10:17 AM


All replies

  • You are using a negative length in any operation. Maybe a string operation were you request a part of the string and you defined length negative!

    Just do what the messages says: Check the stack trace.
    Launch the program in the debugger and see what happens. Usually you should see the wrong code line somewhere in the callstack.
    Monday, October 17, 2005 10:45 AM
  • Thanks for your reply.
    The error i was talking about is happening Inconstantly. It happens all of a sudden in a page and after that i am not able to access any page. But if i replace the application dll i am able to acces the application and the error does not occur if i do the same operation again which caused the errror perviously.

    I am not doing any substring operation in my page. its usually in view pages i get this error. and the beauty is next time i will get this error in some other page.

    please help.
    Monday, October 17, 2005 1:20 PM
  • I am afraid that it would be extremely difficult to speculate what is happening without having a small sample reproducing the issue.

    You might want to try the newsgroups at for more feedback.

      Ayman Shoukry
      VC++ Team
    Monday, October 17, 2005 2:24 PM
  • I am not able to find out where it error occurs. As the code works fine in development environment and when i host it i get this error.
    After getting this error i will replace the dll. then the application will work again adn if i view the same page that caused the error it will work fine but i will get this error in some other page.
    Is it some bug in ASP.NET 
    Tuesday, October 18, 2005 4:11 AM
  • Hi. Would be really interested in more feedback on this issue. We are experiencing exactly the same random error on our server - if we kill the .Net process it goes away, only to come back again a few days later. I am wondering if it's related to a automatic Windows update? We have clients being regularly affected by this on a regular basis. Can anyone suggest an approach?
    Tuesday, February 07, 2006 11:26 AM
  • I am getting the same issue as well. It JUST started happening. It seems to only be happening if my .DLL is in Release mode. ??? If I switch out of Release and compile in debug mode and re-upload the dll to my host ( its works...


    Wednesday, March 01, 2006 12:55 AM
  • Do you have a sample to reproduce the issue?

    Ayman Shoukry
    VC++ Team
    Wednesday, March 01, 2006 1:11 AM
  • Hello

    i got this error inconstantly in my appications page and itself it becoming ok...i belive i got this problem in a mean while i upgrade my dll in the web.....i think u too faced some problem like this

    please let me know the solution.........

    System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown. ---> System.ArgumentOutOfRangeException: Length cannot be less than zero.

    Parameter name: length

       at System.String.Substring(Int32 startIndex, Int32 length)

       at System.Web.UI.Util.GetScriptLocation(HttpContext context)

       at System.Web.UI.WebControls.BaseValidator.RegisterValidatorCommonScript()

       at System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e)

       at System.Web.UI.Control.PreRenderRecursiveInternal()

       at System.Web.UI.Control.PreRenderRecursiveInternal()

       at System.Web.UI.Control.PreRenderRecursiveInternal()

       at System.Web.UI.Page.ProcessRequestMain()

       --- End of inner exception stack trace ---

       at System.Web.UI.Page.HandleError(Exception e)

       at System.Web.UI.Page.ProcessRequestMain()

       at System.Web.UI.Page.ProcessRequest()

       at System.Web.UI.Page.ProcessRequest(HttpContext context)

       at System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()

       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)



    Friday, April 07, 2006 8:02 AM
  • I'm getting the same problem.. I'm not sure where it starts from either.

    It occurs when I am Rendering attributes for a custom control. The only reference to Length is:

    string strAppName = HttpContext.Current.Request.ServerVariables["APPL_MD_PATH"];

    int i = strAppName.IndexOf ("Root/");

    strAppName = strAppName.Substring(i + 4) + "/";

    strCompleteAction = HttpContext.Current.Request.FilePath.Substring(strAppName.Length)+HttpContext.Current.Request.Url.Query;

    Is this because of the way I am using APPL_MD_PATH? It only occurs on one deployment of many, so I think there may be something in IIS or elsewhere affecting this.

    The most difficult part is, I cant replicate the error locally.

    Any ideas would be appreciated!



    Tuesday, April 11, 2006 12:33 AM
  • Please help, i'm also experiencing the same problem. But very strange, it works fine if I load the same page using Firefox.. I can't find the answer anywhere..
    Thursday, October 19, 2006 5:25 PM
  • I do think the problem is because of the substring function which you all are using.
    The Substring function gets the upper lenght which exceeds the maximum length of the string value.
    try might solve.
    Friday, March 13, 2009 10:49 AM
  • I got that problem once because I was using like .IndexOf looking for something that didn't exist in the string. I looked and indexOf was returning a -1 for not found. maybe this helps?
    Thursday, June 11, 2009 9:32 PM