Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server

← PrevNext →

Just recently, I was working on a project that required editing values in an XML file using LINQ. The LINQ query worked fine on my local machine, the code however did not work as expected on the server. It was repeatedly throwing an error saying, “Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server.”

This is an error message raised by the PageRequestManager class that is send by the server in case there is an error. This usually happens when you have added controls inside an <asp:UpdatePanel> control, with or without <asp:AsyncPostBackTigger />.

In addition to this, in the code behind section, I was trying to edit the details in an XML file using XDocument methods. For example,

xml_Doc = XDocument.Load(Server.MapPath("~/data/datafile.xml"));
…
xml_Doc.Save(Server.MapPath("~/data/datafile.xml"));

Customize Error Handling for UpdatePanel

If you are using an UpdatePanel, then you can customize the error handling that often occurs during partial page rendering using the <asp:ScriptManager> element on the page with an UpdatePanel control, and by setting ScriptManager properties.

<asp:ScriptManager ID="scriptManager" runat="server" EnablePartialRendering="true" EnablePageMethods="true" />

To handle the error, simply add the below <script> just below the <asp:ScriptManager> element.

<script type="text/javascript" language="javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    function EndRequestHandler(sender, args) {
        if (args.get_error() != undefined) {
            var msg;
            msg = args.get_error().message;
            console.log(msg);
        }
    }
</script>
You can now see the error message (if any) in your browser’s console. You can use the message to analysis the cause of the error and fix it.

Usually, an error occurs due a bad piece of code that you have written in your code behind section, which the server refused to accept. Find the bug in your code and fix it to avoid the error being thrown.

That's it. Thanks for reading.

← PreviousNext →