locked
Must be non-negative and less than the size of the collection

    Question

  • I keep getting the same error message when i start debugging. This recently started and I don't know why because i didn't change anything yet today.  I could attempt to fix the problem myself but I the error message I get does not tell me where in the code the error is? Does it? It builds fine but freezes on the splash screen. - Thanks

    System.InvalidOperationException was unhandled
      Message="An error occurred creating the form. See Exception.InnerException for details.  The error is: Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"

    Tuesday, October 03, 2006 3:49 PM

Answers

  • well I would start debugging from that splash screen, placing a breakpoint on the form_load event and stepping through the debugger until you get that error and see what's going on :-)
    Tuesday, October 03, 2006 4:32 PM

All replies

  • What is your code on the Form_Load event? perhaps you are doing something to one of the controls causing this problem

    Does this happen when you create a new project also?

    Tuesday, October 03, 2006 3:53 PM
  • Quite a bit happens when I load the form actually, that is why it would be helpful if i knew excatly where the problem is. There are three datagrids populated from a database so I have a hunch the problem is there, Is there somewhere to find out excatly what line of code the problem is in?
    Tuesday, October 03, 2006 4:01 PM
  • well it should normally point it out when you get an exception which you haven't handled in a try catch block but you are stating that it does not go there to the area of the problem so I would suggest start to debug from the first line of code, on every line really or where abouts you think it starts from, place a debug breakpoint at the start of the application, form_load for example and see where the problem happens by stepping through each line.

    normally it does take you to the code where the error happens...

    Tuesday, October 03, 2006 4:04 PM
  • even if I use a catch try on the whole form load the error still comes up. It's right as the splash screen comes up and comes up in the grey box.  When I click the view detail link I still don't get a location for the errror. The program build succcessfully just stop soon after.

    An error occurred creating the form. See Exception.InnerException for details.  The error is: Index was out of range. Must be non-negative and less than the size of the collection.

    Tuesday, October 03, 2006 4:21 PM
  • well I would start debugging from that splash screen, placing a breakpoint on the form_load event and stepping through the debugger until you get that error and see what's going on :-)
    Tuesday, October 03, 2006 4:32 PM
  • I did find this line repeated 20 times in the immediate window of the debug toolbar, if that mean anything to anyone?

     

    A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

    Tuesday, October 03, 2006 4:34 PM
  • yeh thats what the exception is but its now finding it thats the problem. Without seeing code there isnt much we can do
    Tuesday, October 03, 2006 4:37 PM
  •  

     

    There's plenty you can do.

    Often these errors in form load come from an unhandled exception in a routined called by the form load routine. Because of these susceptibilities in form load I keep the form load event as clear as possible.

    But what can you do?

    I haven't run VBE in about 14 months so I don't remember its Debugger Features. I do know that the debugger in more advanced version has Exception Trapping as feature. In other words you can specify that exception and the debugger will trap on it.

    If you have that feature that's what you want to do. Also A's advice to single step through each line of the form load was not bad advice. Single stepping through each line will show you where the exception is.

    As you've already noted, putting the entire form load in a try-Catch serves little purpose other than insuring that only the pre-exception code is executed.

     

    Tuesday, October 03, 2006 5:27 PM
  • I ran across this post looking for a solution to the same problem.  What was happening in my case is that I was getting the same System.InvalidOperationException-Index was out of range error even before the form load event was occurring.  It turns out, like you, I had several datagrids on my form.

    What I found out was causing it was the CellValueChanged event for the datagridview control.  Here is what I was trying to do:

    Private Sub gridMapping_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridMapping.CellValueChanged

              Me.gridMapping.Rows(e.RowIndex).Cells("changed").Value = 1

    End Sub

    As it turns out, this event was firing before the form loaded and at that time e.RowIndex was -1.  All I did was add a simple conditional to circumvent it.

    Private Sub gridMapping_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridMapping.CellValueChanged

            If e.RowIndex >= 0 Then

                   Me.gridMapping.Rows(e.RowIndex).Cells("changed").Value = 1

            End If

    End Sub

     

    Tuesday, November 21, 2006 7:22 PM
  • ya even i am facing the same problem for Enterprise Search in MOSS 2007 in
    search scope, all crawling has been done successfully and updating scopes are
    even successfull but when it comes on the site-->search scope
    setting-->search drop down when clicked on search dropdown it throwing the
    same error
    Index was out of range. Must be non-negative and less than the size of the
    collection. Parameter name: index.
    this is not the problem with central administration bcos all crawling and
    scopes updating has been successfully, problem lies with site page called
    "scopedisplaygroup" pages. anybody if had worked on it pls update it, bcos i
    am not facing these things in MOSS 2007 ENTERPRISE EDITION but i am facing
    these things in MOSS 2007 FOR SEARCH FREE TRIAL VERSION
    Thursday, June 28, 2007 7:32 AM
  • I get the same error in the free trial version of MOSS 2007...
    Wednesday, January 02, 2008 5:14 PM
  • By reading your exception..."......less than the size of the collection"....it talks about size.......so what you can do is

    replace int / string / object to type long. When you use long then it can store much bigger values.

     

    Usually if the array couldnt store big size data it throws that exception. So use "long" in place of int/string that should solve your problem.

     

    Monday, January 14, 2008 10:44 PM
  • Thankyou, Thankyou


    For weeks I have been runninig into the same expection error, with no help anywhere. I have 3 datagrids on one form.

    It would throw the exeption when I started the debugger, and I was trying line by line debug, starting at the load event with no solution.  Then I read your post, and decided to start the line by line debug at the very first DIM statement at top of my form1.vb. It picked up all the varibles ok, and then it went to the dataset pages and table adapter pages,  but then it went to my form1.vb page to the sub_Datagridview1_ColumnWidthChanged sub, (I am comparing 2 datagrid column widths and the 2nd datatgrid is not loaded yet! BINGO!!!

    This happens BEFORE the Load event.


    My point,  there are lots of events before theLOAD event, and be sure to start the line by line Debug at the first Dim statement in your Code, when you are trying to solve this problem.

    Thanks

    Rehabman


    Saturday, April 05, 2008 2:05 PM
  • Hallo,

    I am having the same error. I have different datasets and datatables. I have two different threads that are writing and reading to and from these datatables. Strange enough the error ocurs at different times. If i click play then it goes well for a while and then again the exception. I guss that i may read and write at the same time using different threads. But i dont know why the exception ocurs cause the datasets have the correct row quantities.

    Someone have any idea?

    Regards
    Monday, November 16, 2009 11:01 AM
  • You try to run the page in Firefox?
    Friday, February 12, 2010 6:04 PM
  • Thanks a million Natalie_Wieland!

    Exactly the same thing happened to me, i put in the condition and it works perfect! Not sure why it started doing this as I had been using the app for quite some time.

    Thanks!

    Thursday, April 29, 2010 3:12 PM