Showing posts with label exception. Show all posts
Showing posts with label exception. Show all posts

Monday, March 26, 2012

Will Application_Error catch any unhandled exception during atlas postback ?

Looks like atlas postback unhandled exception is not comming to Application_Error handler.

Is this right ? or is there any way to redirect any atlas postback unhandled exception to "Application_Error" ?

Thanks,

Perhaps you can utilize the Error Template tag as well as OnPageError event to handle exceptions you encounter in your application.

<atlas:ScriptManager EnablePartialRendering="true"OnPageError="Page_ErrorHandler" runat="server">
<ErrorTemplate>
<div style='width: 450px; height: 300px; padding: 10px; border: solid 3px black; background: #ffd; text-align: left;'>
<h1>Server Error</h1>
<p>Oops looks like we're still working on some code. Administrators have been notified and this emerging issue will be fixed.</p>
<p><input id="okButton" type="button" value="OK" runat="server"/></p>
</div>
</ErrorTemplate>
</atlas:ScriptManager>

Here is the handler for the error.

Import System.Net.Mail

C#

protectedvoid Page_ErrorHandler(object sender, PageErrorEventArgs e)
{

MailMessage mail = new MailMessage();
mail.From = new MailAddress("me@.mycompany.com");
mail.To.Add(you@.yourcompany.com);

mail.Subject = "Error Occured My Application";
mail.Body = "Error Message: e.Error.Message<br /><br />Stack Trace: e.Error.StackTrace<br /><br />Source: e.Error.Source<br /><br />Help Link: e.Error.HelpLink";
mail.IsBodyHtml = true;
smtp.Credentials = new NetworkCredential("username", "secret");
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

//Or perhaps insert it into a database. Or if you feel like it... perhaps the Windows Event Viewer

}

VB

SubPage_ErrorHandler (sender As Object, e As PageErrorEventArgs)

Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress("me@.mycompany.com")
mail.To.Add("you@.yourcompany.com")
mail.Subject = "Error Occured My Application"
mail.Body = "Error Message: e.Error.Message<br /><br />Stack Trace: e.Error.StackTrace<br /><br />Source: e.Error.Source<br /><br />Help Link: e.Error.HelpLink"
mail.IsBodyHtml = True
smtp.Credentials = New NetworkCredential("username", "secret")
Dim smtp As SmtpClient = New SmtpClient("127.0.0.1")
smtp.Send(mail)

End Sub

You can also throw the credentials into the web.config inside the <System.Net> tag like so.

<system.net>
<mailSettings>
<smtpfrom="me@.mycompany.com">
<networkhost="127.0.0.1" port="25" userName="myUsername" password="secret" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>

Hopefully that will help a little bit.

JoeWeb


Proposed solution is great. But it requires to create Page_ErrorHandler in every Page whereScriptManager is present. It leads to duplication of code. And it very easy to miss it somewhere.

We're logging all unhandled exception in ASP.NET application onproduction environment and regulary analyze them. Errors that occur during Atlas post-back are not handled because Application_Error is not fired.

Is there any solution to exception handling during Atlas post-backs like Application_Error which is global for all application?

Thanks in advance


What about this? Implement your error handling method for the script manager event as static in a class, and onload of any page with the script manager, all you have to do is assign that static method to the event handler... then you won't be duplicating any code, and its only 1 line of code to add per page.


The ErrorTemplate is no longer supported with Ajax and these are two methods you can use..

In codebehind - I always pull in the scriptmanager so that I can ref it and manage it.

( on your page init)

SM = (ScriptManager)GetControl(skin,"SM");
SM.AsyncPostBackError +=new EventHandler<AsyncPostBackErrorEventArgs>(SM_AsyncPostBackError);

then new event...

void SM_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
SM.AsyncPostBackErrorMessage = e.Exception.Message;
bllLogging.RecordError("GridViewModal ScriptManager Error", e.Exception, Severity.Severe, "", "");
}

The BllLogging is my custom error recording routine I use to track error - replace with your own or re-throw it if you have a global exception handler...

In the example above - I use this in a base class which all of my other classes derive from so I only have to code it once...

Alternatively you can also do this on your pages or include in a mycustom.js

<ajax:ScriptManager ID="SM" EnablePartialRendering="true" ScriptMode="Auto" runat="server" />
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnEndRequest);

function OnEndRequest(sender,args)
{ // *** Check for errors

if (args.get_error() != undefined /* && args.get_error().httpStatusCode == '500' */)
{
//debugger;
var errorMessage = args.get_error().message
alert("Custom Error Handling:\r\n" + args.get_error().message);
args.set_errorHandled(true);
//var ErrorDisplay = $get("ErrorDisplay_Message");
//ErrorDisplay.innerHTML = "Custom Error Handling:\r\n" + args.get_error().message;
}

}
}
</script>

There also some additional things you can do with assigning custom errors and the likes and is well documented on their documentation site under scriptmanager references...

However, be careful when using the custom error feature - the code behind version does quite well logging actual messages but when in custom mode - whatever is provided as the error text is used (which hides the actual error message...)

Saturday, March 24, 2012

Wot is this error "DynamicControlID must be set "

Server Error in '/AJAXEnabledWebSite1' Application.

DynamicControlID must be set

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.ArgumentException: DynamicControlID must be set

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:

What ajax components do you have on the page you are running? Most likely one of them is missing a control association. For example the modalpopupextender has to have a dynamiccontrolID set

it's the web control to associate the AJAX Extender with... look through all the ajax components on the page and check to see if any of them are missing a "DynamicControlID" And if they have that as a property that can be set

Wednesday, March 21, 2012

Writing New Control - WebResource Url Throwing Exception

Hey Everyone,

I'm having a really hard time with this one. I recently upgraded to lateest control toolkit, and now i can't use my custom controls. I get the dreaded unrecognized tag error.

When I try to load the webresource.axd url for the javascript, I'm getting the following error:

WebResource.axd?d=_IYF6c389WdEZP8BREten0VCyRTYFXUFn9GR9MRXP-aoOJ5cYUZwsxAz4rdBOPaz6CoHQyUbH48-Kn0gv5WhhCj2yI8nUNAt-hz-wRrJAwI76fBROssmxDJplsNW85vw0&t=632907313964152042

Specified argument was out of the range of valid values.
Parameter name: offset

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.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: offset

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:

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: offset]
System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count) +3153595
Microsoft.Web.Services.WebResourceCompressionModule.OnPreRequestHandlerExecute(Object sender, EventArgs e) +1103
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64


any ideas?For some reason, the Atlas Control Extender template doesn't make the Behavior.js an embedded resource. Change that in the properties pane and rebuild.
yeah, i saw that in the FAQ, and made sure (and double sure) that it was marked as an embedded resource. and it looks like it is, because i am seeing the resource in the source. it's when the browser tries to read the resource that .NET is throwing that funky WebResourceCompressionModule error...
So using a tool like Fiddler, the js file is being sent across?? Hmmm.
I actually saw this yesterday when I was doing some work but (unfortunately) I didn't ever figure it out, mostly because it kind of magically went a way. I think I rebuilt everything clean and it started working.

WYSIWYG-editor saying that a ScriptManager is not present when it is; throws an exception

Hey Guys,

I'm new to the AJAX Toolkit, so pardon me if my problem seems a bit dumb.

WYSIWYG Exception

As you can see in this image, I have a ScriptManager on the page, so I don't have a hot clue of what's really causing the problem. My HTML is all valid, and I can actually run the page through the built-in server and it works just fine.

Anyone have any ideas?

I'm not so sure but I think this could be caused by a wrong reference to the library that ajax uses (the Web.Extentions).

Try to remove the scriptmanager and just drag a new ScriptManager from the ToolBox to the top of the webpage. Then save the page, close it, and reopen it. Normally the design-view should render normally now.

I hope this helps!

Greets,
Wim