Windows Develop Bookmark and Share   
 index > Windows Forms General > System.ArgumentOutOfRangeException: Length cannot be less than zero.
 

System.ArgumentOutOfRangeException: Length cannot be less than zero.

Hi,

I get the following error whenever i try to run the exe.
I did not get this error opn the development machine.
Any clue?
It points to length parameter but i dont see anything wrong there.
I checked the length parameters in those forms.
Is it caused by a substring?



See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at trTransportationMgrProject.cls_Schedules.getSelectCriteria()
at trTransportationMgrProject.cls_Schedules.loadGDVData()
at trTransportationMgrProject.frm_Schedules.btnSearch_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3074 (QFE.050727-3000)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
trTransportationMgrProject
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Default%20Company%20Name/Setup1/trTransportationMgrProject.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
trBaseControls
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Default%20Company%20Name/Setup1/trBaseControls.DLL
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
PresentationCore
Assembly Version: 3.0.0.0
Win32 Version: 3.0.6920.1500 built by: QFE
CodeBase: file:///C:/Windows/assembly/GAC_32/PresentationCore/3.0.0.0__31bf3856ad364e35/PresentationCore.dll
----------------------------------------
WindowsBase
Assembly Version: 3.0.0.0
Win32 Version: 3.0.6920.1453 built by: NetFX
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/WindowsBase/3.0.0.0__31bf3856ad364e35/WindowsBase.dll
----------------------------------------
trUtilities
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Default%20Company%20Name/Setup1/trUtilities.DLL
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3074 (QFE.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
AxInterop.MapPoint
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Default%20Company%20Name/Setup1/AxInterop.MapPoint.DLL
----------------------------------------
System.Core
Assembly Version: 3.5.0.0
Win32 Version: 3.5.30729.1 built by: SP
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
Interop.MapPoint
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Default%20Company%20Name/Setup1/Interop.MapPoint.DLL
----------------------------------------
trUserControls
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Default%20Company%20Name/Setup1/trUserControls.DLL
----------------------------------------
-digqx4h
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

rowter  Wednesday, August 26, 2009 8:11 PM
Can you show us that line of code? For some reason, either the string (first argument) or the length (second argument) is different on your test machine.

I've normally seen this when people try to "split" a string themselves. Something like:

string results = originalString.Substring(position, originalString.Length-position - 1);

If you're doing that or something similar, your "position" argument may be at or past the end of the string, which causes the second argument to be <0.
Reed Copsey, Jr. - http://reedcopsey.com
  • Marked As Answer byrowter Thursday, August 27, 2009 4:23 PM
  •  
Reed Copsey, Jr.  Wednesday, August 26, 2009 8:50 PM

Excellent! Then you will never have an error on that line! :-)

Just kidding.

If that is indeed where the error is occurring, then somehow it is getting to be less than 2 at runtime.

If you want to post a little more of your code, we can help.

Otherwise, you could consider putting more logging code into your application to display this value into a log at runtime when the code generates the exception you defined at the beginning of this thread.

Hope this helps.


www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Marked As Answer byrowter Thursday, August 27, 2009 4:23 PM
  •  
DeborahK  Wednesday, August 26, 2009 10:14 PM
That would be a good guess.

Do you have some substring logic in getSelectCriteria?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, August 26, 2009 8:25 PM
Somewhere in your routine: trTransportationMgrProject.cls_Schedules.getSelectCriteria

You are calling string.Substring, and passing a value <0 for the second argument. Unfortunately, that is all your stack trace shows.

If you want to post this code, we can help narrow it down. Look for calls to Substring. Chances are, you're passing a value there being built off the string, and it's ending up with a value <0.
Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Wednesday, August 26, 2009 8:26 PM
I have a substring that i am using in getselectcriteria function but when i am debugging(on developmet machine), i can see te value of it to be more than 0.
On the development machine this works fine without error. error comes up only whne i run the exe on the test amchine
rowter  Wednesday, August 26, 2009 8:46 PM
Can you show us that line of code? For some reason, either the string (first argument) or the length (second argument) is different on your test machine.

I've normally seen this when people try to "split" a string themselves. Something like:

string results = originalString.Substring(position, originalString.Length-position - 1);

If you're doing that or something similar, your "position" argument may be at or past the end of the string, which causes the second argument to be <0.
Reed Copsey, Jr. - http://reedcopsey.com
  • Marked As Answer byrowter Thursday, August 27, 2009 4:23 PM
  •  
Reed Copsey, Jr.  Wednesday, August 26, 2009 8:50 PM
Do you have any calculations in the substring?

Could you post that bit of code?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, August 26, 2009 8:50 PM

strSelect has column names in it. (string values)
In the debug mode when i do a quick watch on length it says length is not declared.

length is showing the total count of characters in strselect.

this is the line of code:

strSelect = strSelect.Substring(0, strSelect.Length - 2)

rowter  Wednesday, August 26, 2009 9:04 PM
On your test machine, strSelect.Length is < 2, so you're passing -1 or -2 to that routine.

You can verify this - just add something like:

if (strSelect.Length < 2)
Console.WriteLine("strSelect's [{0}] length is only {1}! We're going to crash now.", strSelect, strSelect.Length);
strSelect = strSelect.Substring(0, strSelect.Length - 2);



Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Wednesday, August 26, 2009 9:08 PM
Then at run-time it appears that strSelect.Length is less than 2.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, August 26, 2009 9:08 PM
strSelect is not going less than 2
it it not going into that loop.
rowter  Wednesday, August 26, 2009 10:08 PM

Excellent! Then you will never have an error on that line! :-)

Just kidding.

If that is indeed where the error is occurring, then somehow it is getting to be less than 2 at runtime.

If you want to post a little more of your code, we can help.

Otherwise, you could consider putting more logging code into your application to display this value into a log at runtime when the code generates the exception you defined at the beginning of this thread.

Hope this helps.


www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Marked As Answer byrowter Thursday, August 27, 2009 4:23 PM
  •  
DeborahK  Wednesday, August 26, 2009 10:14 PM
I think i found where the error is .... i have to test it tomorrow.
strselect.length should get values from a table. which might be empty on the test machine.
thanks for your help .

rowter  Thursday, August 27, 2009 1:14 AM
This issue was fuixed when i populate the table.
Thanks.
rowter  Thursday, August 27, 2009 4:23 PM

You can use google to search for other answers

Custom Search

More Threads

• Getting Control in Main Window
• Show message on splash screen
• clearing dataGridView?
• Windows Constants Posted
• How can I create a custom print footer in csharp for Webbrowser control?
• passing data between 2 datagridviews
• Help with classes
• DateTimePicker.Value not updating when using AcceptButton
• Control bind to object does not refresh it's display
• DataGridView Remove VS Visible = False