21

I have radcombo boxes on aspx page, and when I select any option from then it gives error`Server Error in '/' Application.

Operation is not valid due to the current state of the object. 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.InvalidOperationException: Operation is not valid due to the current state of the object.

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:

[InvalidOperationException: Operation is not valid due to the current state of the object.]
System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded() +2692302 System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) +61
System.Web.HttpRequest.FillInFormCollection() +148

[HttpException (0x80004005): The URL-encoded form data is not valid.] System.Web.HttpRequest.FillInFormCollection() +206
System.Web.HttpRequest.get_Form() +68
System.Web.HttpRequest.get_HasForm() +8735447
System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) +97 System.Web.UI.Page.DeterminePostBackMode() +63 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +133


Version Information: Microsoft .NET Framework Version:2.0.50727.3634; ASP.NET Version:2.0.50727.3634 `

peterh
  • 11,875
  • 18
  • 85
  • 108
G.S Bhangal
  • 3,060
  • 4
  • 23
  • 48
  • 2
    It would e helpful if you can post some code. But this might be your issue: If your RadComboBox posts back on selection and you have a lot of form fields then you might get this exception. Here is another question with more details http://stackoverflow.com/questions/8832470/operation-is-not-valid-due-to-the-current-state-of-the-object-error-during-pos – Fahad May 22 '12 at 07:28

3 Answers3

55

From http://codecorner.galanter.net/2012/06/04/solution-for-operation-is-not-valid-due-to-the-current-state-of-the-object-error/

Issue happens because Microsoft Security Update MS11-100 limits number of keys in Forms collection during HTTP POST request. To alleviate this problem you need to increase that number.

This can be done in your application Web.Config in the <appSettings> section (create the section directly under <configuration> if it doesn’t exist). Add 2 lines similar to the lines below to the section:

<add key="aspnet:MaxHttpCollectionKeys" value="2000" />
<add key="aspnet:MaxJsonDeserializerMembers" value="2000" />

The above example set the limit to 2000 keys. This will lift the limitation and the error should go away.

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
11

This can happen if you call

 .SingleOrDefault() 

on an IEnumerable with 2 or more elements.

Herman Schoenfeld
  • 8,464
  • 4
  • 38
  • 49
  • 2
    You get "InvalidOperationException: Sequence contains more than one element" – ps. Jan 08 '13 at 21:28
  • Not on Mono. My answer is correct and shouldn't have been down voted. – Herman Schoenfeld Jan 09 '13 at 00:54
  • At best, this answer should be a comment and off-topic; at worst, it's just plain wrong and not helpful since the user reported a stack trace with HttpValueCollection. – Lynn Crumbling Feb 22 '13 at 04:29
  • 11
    Lynn, this exception occurs on SingleOrDefault() in Mono. Whilst it might not pertain to this question 100%, it does relate to it. It is useful for web-surfers who can search for this exception message (and who use mono) and find my solution, rather than waste time. In fact, that seemed to be the case for several users but the nanny brigade have come through here and down-voted without considering the reality of the situation. This site is to help developers, not social network for nannies to chop down relevant answers. It's detrimental to this site and it doesn't help legit developers. – Herman Schoenfeld Feb 22 '13 at 04:39
  • What you aren't grasping is that this person has asked a question about a VERY SPECIFIC problem that they were having. They provided details and were looking for an answer to their issue. One of the symptoms happened to be that they got a message `System.InvalidOperationException: Operation is not valid due to the current state of the object.`, but that's all it is, a symptom. It is not relevant for you to post an answer that describes something else that produces this symptom, since it doesn't answer the original question. – Lynn Crumbling Feb 22 '13 at 17:36
  • To put it another way - Think of it as this: I ask the question "what is 2+2?" I would expect someone to post an answer that says: The answer is 4. It is not relevant to post an answer saying "1+3 also produces this". – Lynn Crumbling Feb 22 '13 at 17:38
  • Also - just to be clear, I was not one of the people who downvoted this. @ps Herman is correct that mono produces different exception text, but it doesn't really matter anyway. – Lynn Crumbling Feb 22 '13 at 17:39
  • I was the one who down voted. And I really thought I was doing the right thing. So to save the good folks on the inter-web from wasting their time on something this insignificant i am willing to change my down-vote. @Herman the system doesn't let me change my vote unless the answer is edited. – ps. Feb 25 '13 at 19:57
  • 1
    Thank you, Herman. I'm using Mono as well and your answer was very helpful. @LynnCrumbling, I'm not sure how this should best be handled in StackOverflow, but this answer was absolutely helpful and I found it searching for the exact error message in the title. – Ken Pespisa Jul 14 '13 at 01:45
  • 3
    @KenPespisa Hello, based on your rep, you sure aren't any stranger to SO... I guess I'll put the issue that I have in the context of a mock example: If you come across a post that specifically says "I'm using Fortran and seeing XYZ error" and someone posts a response saying "This happens in Cobol too when you have a bad pointer!", you're going to downvote; the question was not tagged Cobol, so the answer is irrelevant. BUT! Just because the answer is not pertinent to this question doesn't mean that it isn't useful to someone. Why not start a new question, that the answer fits, and answer it? – Lynn Crumbling Jul 15 '13 at 02:23
  • 1
    You're right, that is the best option. I had to think about it a while. This is the first time I've encountered this type of situation. Adding the error message to the new question title and tagging it Mono would make it right. – Ken Pespisa Jul 15 '13 at 11:39
1

I know an answer has already been accepted for this problem but someone asked in the comments if there was a solution that could be done outside the web.config. I had a ListView producing the exact same error and setting EnableViewState to false resolved this problem for me.

Elaine K
  • 507
  • 2
  • 7
  • 32
  • I had the same error upon calling a record from DB with `FirstOrDefault()` using anonymous variable(`var recordInfo = dc.`). I already do have the web config property ``. It is solved upon using proper model for variable(`MyModel recordInfo = dc.`). – Pranesh Janarthanan May 02 '18 at 13:25