Welcome

Hello, Welcome to my blog. If you like feel free to refer others

Thursday 18 October 2012

'Operation is not valid due to the current state of the object' error during postback

Whenever a postback is done, this error occurs when form fields are very large in number. The stack trace is:
at System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded() at System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) at System.Web.HttpRequest.FillInFormCollection()
By default, the maximum value of MaxHttpCollection is 1000.
To solve this error, increase the MaxHttpCollection value. Try adding the following setting in your web.config's <appsettings> block.
<appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="2001" />
</appSettings>
 
It can solve your problem. If you have very large records on your page, say 600 records in a grid, and you click on Submit, then increase the value of MaxHttpCollection.
Then change value 2000 to 5000 and test again. It will be resolved. 

Question : Specifying MaxHttpCollectionKeys for a specific page - is this possible? 

No, that's not possible. It always checks the appSetting aspnet:MaxHttpCollectionKeys and if that is missing it will be set to 1000. You cannot override this because the reading of the setting and the default value is done inside an internal class.

Perhaps a better approach would be to build a form with less than 1000 items. Maybe in the form of a wizard. Or maybe by combining multiple values into a single data structure and deserializing it on the server.

for more info please visit the link

Happy learning....... :)


No comments:

Post a Comment