Mysterious build error: Invalid argument. Length cannot be less than zero. Parameter name: length RRS feed

  • Question

  • I'm one member of a team of about a dozen developers working on a very large solution (almost 200 projects) in Visual Basic .NET 2005 (Team Edition for Software Developers, with SP1 installed, using Visual SourceSafe).  This solution has been in development for almost two years and I have been working on it for about a year and a half.  Yesterday, while attempting to debug the solution in the IDE, the following error appeared, associated with the startup project:

    Invalid argument. Length cannot be less than zero. <my startup project name>
    Parameter name: length

    Clicking on the error doesn't open any code windows.

    I've taken the following steps in vain:

    Clean and rebuild the startup project
    Clean and rebuild the entire solution
    Retry using MSBuild
    Close the IDE, delete all the files in the Project Assemblies folder, then reload the solution and retry the build
    Close the IDE, delete all the debug (*.pdb) files on the machine, then reload the solution and retry the build
    Delete the project from it's folder on the local drive, then re-load it from Visual SourceSafe, then retry the build

    I've seen several references to this error in forum posts, etc., but no concrete information on what conditions actually cause it or how to resolve it.  It's wasting a great deal of my time.  Has anyone experienced this error?  Does anyone know what causes it and how to resolve it, other than cleaning and rebuilding the project/solution?

    Thanks in advance



    Tuesday, February 20, 2007 6:37 PM

All replies

  • The only time I have gotten this error was when I had accidentally set the value of a textbox value to null in one sub, then tried to use it's value in a sub that executed right after that to pull info from a database based on the ID number listed in the textbox.
    Wednesday, February 21, 2007 12:13 PM
  • I've been getting this error on and off. I have no idea what causes it, nor what rectifies it. It seems to come and go on it's own.


    I've been looking for a way to resolve this.


    Anyone had any luck finding a resolution?


    Tuesday, August 14, 2007 7:33 PM

    If you REBUILD (BUILD does not work) your project/solution it seems to clear the problem. Another undocumented feature!!!!
    Monday, September 10, 2007 1:18 PM
  • Have you tried right-clicking any project in the solution explorer and selecting the "Set as Startup Project" option? It seems that your solution lost the startup project.


    Hope this helps,


    Tuesday, September 18, 2007 1:24 AM
  • I had no errors and was running my app happily in the IDE. I was debugging a line of code and realised the mistake I had made, so I clicked stop, made the amendment and when I built again I got this error.
    I got the error twice in two projects, that were in the same solution.
    I clicked rebuild rather than build and got about 90 errors.
    I then clicked build and just got errors about my custom controls.
    I added the custom control projects in and made the references point to the projects rather then the binarys and everything was fine.

    What a PITA tho.
    Monday, September 24, 2007 9:41 AM
  • I started getting this same error in a 2005 solution that I'd been working on daily. I found that one of my projects had an app.config file associated to it. it was working normally. a coworker deleted that app.config file from the project, and it compiled for a couple of hours, and then suddenly started giving this error. after going through all the steps everyone else did, and still receiving the error, I went to my recycle bin and restored it, added it back to the project, and it started working normally again.
    In my case, rebuild would make the error go away, but the moment I tried to step through the code, it returned. Only restoring the app.config file fixed the issue, which I thought, was weird, because I thought only applications, and not DLL’s need a config file. I assumed it was a change in 2005 where the step through process needs the config.
    if it makes a difference, my 2005 project references dlls built under the 1.1 framework and the 2.0 framework as well.


    Tuesday, November 20, 2007 1:57 PM
  • Hi,


    The only time I have encountered that error is if you let the Length parameter of the String.Substring method fall below zero.


    Here is an example. I have added a RichTextBox at run-time so you don't need to add any controls to your FORM.









    To correct the error in the code below change the highlighted minus one ( -1 ) to zero ( 0 ).



    Code Block


    Option Strict On

    Option Explicit On

    Public Class Form1


    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.RichTextBox1 = New System.Windows.Forms.RichTextBox





    Me.RichTextBox1.Location = New System.Drawing.Point(12, 12)

    Me.RichTextBox1.Name = "RichTextBox1"

    Me.RichTextBox1.Size = New System.Drawing.Size(450, 425)

    Me.RichTextBox1.TabIndex = 0

    Me.RichTextBox1.Text = ""




    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

    Me.ClientSize = New System.Drawing.Size(474, 449)


    Me.Name = "Form1"

    Me.Text = "Form1"


    Dim teststring As String = "The quick brown fox jumps over the lazy dog!"

    Dim outputString As String = ""

    For index As Integer = teststring.Length - 1 To -1 Step -1


    'The following line will throw the error when index = -1 ->>>>

    outputString &= teststring.Substring(0, index) & ControlChars.NewLine


    RichTextBox1.Text = outputString

    End Sub

    End Class





    Tuesday, November 20, 2007 5:19 PM
  • I  had the same problem, just out of nowhere.
    Got solved by setting my startupproject, although it still was set, and using REbuild. instead of build. Another nice error brought to you by microsoft I should say...
    Monday, November 26, 2007 9:00 AM
  • What a bizare error this is. As per most of those above, I had changed nothing in my code and tried to start my app in the IDE and received this wierd error. After scouring the web, found this suggestion to REBUILD.

    Many thanks to those suggesting this, I would never have thought to try this!
    Tuesday, June 17, 2008 4:04 PM
  •  I have this exact same problem.  I have a very small, simple C# .NET 2003 app that checks for a directory 

       stFolder = System.Configuration.ConfigurationSettings.AppSettings["TheFolder"].ToString() ;

    //Make sure the folders end in a \
    if (strFolder.Substring(strFolder.Length-1,1) != "\\" ) strFolder += "\\";

        DirectoryInfo di =
    new DirectoryInfo(strFolder);
        FileInfo[] fi = di.GetFiles();
    foreach (FileInfo fiTemp in fi)
    throw new ArgumentException ( "Invalid folder specified in the config file.");

    throw ;


    It works perfectly in the IDE.

    So then I create an Install MSI and install the package.  Now if I change the config file for the strFolder to an invalid folder it throws this terrible error message that we are talking about.

    The only way I got around this was to Rebuild the application in DEBUG (RELEASE never works).  This willl create the debug pointer file called *.pdb.  If you include this pdb file in your installer or just copy it to the installed folder you will not have this issue.

    So.... how to create a RELEASE version of the exe without getting this usless and unneed error mesage?  Why is it ignoring my try catch logic?

    I am going to post this on a new thread and hope MS support can reproduce this issue and suggest why this is happening.
    Monday, August 25, 2008 6:11 PM