Index out of range, ThrowArgumentOutOfRangeException

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 17-Jan-2007 06:37:07   

Otis, I am getting an intermittent ThrowArgumentOutOfRangeException in a ASP.NET. I have looked through all related messages for this and it appears you have fixed several bugs around this same issue (search for ThrowArgumentOutOfRangeException - there are 5 related threads). In my particular case, I CANNOT reproduce it. My web application logs this error and I get anywhere from 0-5 of these errors per day.

I was using the builds/templates/etc from 12-05-2006 of 2.0. I have just downloaded the latest templates and library code to see if that helps, but I don't see anything in the change logs about this issue.

The error stack is:


System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at SD.LLBLGen.Pro.ORMSupportClasses.CollectionCore`1.get_Item(Int32 index)
   at Wizard.StepMain_Deactivate(Object sender, EventArgs e)
   at System.Web.UI.WebControls.View.OnDeactivate(EventArgs e)
   at System.Web.UI.WebControls.MultiView.set_ActiveViewIndex(Int32 value)
   at System.Web.UI.WebControls.Wizard.set_ActiveStepIndex(Int32 value)
   at System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e)
   at System.Web.UI.WebControls.Wizard.WizardChildTable.OnBubbleEvent(Object source, EventArgs args)
   at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception stack trace ---
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.wizard_aspx.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

The code where it fails, loads an existing collection with some data. In my case, I have a field that determines how many 1:n child records I should have. So, I override the anEntity.RelatedCollection property and make sure the collection contains at least the number of child records. I then loop through them. It is in this loop where I get the error - even though I have just verified the correct number is present:

My override code is simple like:


        public override a1NRelatedCollection RelatedCollection
        {
            get
            {

                if (base.RelatedCollection.Count < RelatedQuantity)
                {
                    for (int x = 0; x < RelatedQuantity- base.RelatedCollection.Count; x++)
                    {
                        base.RelatedCollection.Add(new RelatedEntity());
                    }
                }
                return base.RelatedCollection;
            }
        }

and the code that loops is like:


    int quantity=10;
    anEntity.RelatedQuantity = quantity; //make sure it knows how many we need!
    a1NRelatedCollection someList = anEntity.RelatedCollection;
    for (int x = 0; x < quantity; x++)
    {
        someList[x].SetNewFieldValue(3, "somevalue");
    }

(note, I guess I could use a "foreach loop" - but I would still have to have iteration variables for other uses in the code, so that is why it is a "for loop" - should work though!)

Again, the code works most of the time and I cannot track down what the problem might be. I figured it was in LLBLGen, since I know the collection size and verify it right before I loop through it.

Any ideas or suggestions?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 17-Jan-2007 07:53:52   

I think your code should work, but would you please try the following code:

        
        public override a1NRelatedCollection RelatedCollection
        {
            get
            {
                while (base.RelatedCollection.Count < RelatedQuantity)
                {
                     base.RelatedCollection.Add(new RelatedEntity());
                }
                return base.RelatedCollection;
            }
        }


int quantity=10;
anEntity.RelatedQuantity = quantity;
a1NRelatedCollection someList = anEntity.RelatedCollection;
    for (int x = 0; x < RelatedCollection.Count; x++)
    {
        someList[x].SetNewFieldValue(3, "somevalue");
    }

Also, which runtimeLibrary version are you using?

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 17-Jan-2007 16:22:26   

Also, which runtimeLibrary version are you using?

I indicated I was running the ones from 12-05 - I think that makes the version 2.0.0.61205. I have updated to the 2.0.7.111 version now is why I cannot go get the version right now - but when I was writing the email - that is how I came up with the date of everything I was using.

Anyway, since there is a problem with having the right number of objects in the collection - I can see how your code might now cause it all to work - however, this will actually just MASK the real problem. It will mask it by me no longer getting an exception, but there will be missing data - I think I would rather have the exception that way the user does not even think it worked.

Having said that, I will put your code in place and see if the exception goes away. If it does go away, what does that prove and how does that help (since there will not be an exception, but also - a RelatedQuantity of objects will NOT be updated - it will be something less...)?

Thanks for your help!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 17-Jan-2007 16:28:32   

Let's see first if the exception goes away, then we may trace out why it didn't work the first time. Maybe something somewhere was messing with the collection.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 01-Feb-2007 09:04:54   

Exception is gone now. Unsure on the data. Wonder how/why this was not working before?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Feb-2007 09:22:31   

Exception is gone now

That's good news

Wonder how/why this was not working before?

disappointed mmmm, be positive and look ahead smile