Friday, September 19, 2008

System.MethodAccessException - System.Data.Common.DataRecordInternal.get_Item(System.String)

Ever wondered what this exception is? I ran into this issue last week when i deployed my code in a shared hosting environment which was running in Medium trust. It was working fine on my local. After some research i found that this error was thrown by a Repeater control on a page whose datasource was a datareader. In a Medium trust environment, according to microsoft "Reflection is not allowed". Looks like when you assign a datareader to a repeater control internally it is doing some reflection which is causing this issue. Another strange behaviour with this error is it won't even tell you the exact error, instead it will be displaying something like this in a shared hosting,

Security Exception

Description:

The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.


Exception Details: System.Security.SecurityException: Request failed.


So how will you reproduce this error locally, there is setting you can add it to your web.config


<system.web>
    <trust level="Medium" />
</system.web>

So the workaround i have found for this issue is to use dataset instead on datareader. Let me know if you guys have found a different solution.

1 comment:

  1. Thank you! My code was working for years and then all of a sudden I got this error. I changed my datareader to a dataset and it worked! I can go to sleep now! Thank you again!

    ReplyDelete