Link to home
Start Free TrialLog in
Avatar of wibleb
wibleb

asked on

Capturing Error Message in an Alert Box

When my application throws a specific error I want the full stack trace to appear in an alert box that the user can see and once they hit OK the screen is not affected (in otherwords I don't want it to show the error page).  

I have a solution that gets pretty close to working but I am having problems with it taking certain parts of the text as key symbols.  For example, if the full stack trace shows a directory it will take the '\B' in  c:\Backup to mean something else.  

Below is the code I have so far.  I have tried playing with Server.URLEncode and Decode and have not been able to get that to work the way I want.   Any insight would be greatly appreciated.

try
{
SomeMethod();
}
catch (Exception ex)
{
DisplayAlertBox(ex.ToString());
}


public void DisplayAlertBox(string message)
{
                  
   string popupScript = "<script language='javascript'>" +
   "window.alert(\"" + message +       "\");" +      
   "</script>";                  

   Page.RegisterStartupScript(message, popupScript);

}      
SOLUTION
Avatar of AerosSaga
AerosSaga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wibleb
wibleb

ASKER

I am only familiar with using Validation Summaries to display problems with invalid entries in forms.  What if the error I want to display is something in my business logic or maybe an error due to Database timeout.  How do I get the TimeOut Error to display in the Validation Summaries message box.  

I have inserted a control called ValidationSummary1 on my ASP.net page already.  What would I put in my catch block to get a message box to result from this Validation Summary.

try
{
   SomeMethod();
}
catch (Exception ex)
{
   ????????
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops...  I didn't test it before sending. It's not gonna work. The reason is that, the error message is in the server. You need to make another post back to get the message. So you can't really capture the message.
-Baan
Avatar of wibleb

ASKER

I couldn't get any of your suggestions to work but I found a workaround which seems to work ok.  

ValidationSummaries with a custom validator is a good thought but the MessageBox feature only appears to work on ClientSide exceptions.  When you set the IsValid to False on the Server side the control ignores the MessageBox setting.  

I tried every possible way to use URLencode and HTMLencode and could not find a solution.  

What I finally did that worked alright was to modify my initial Method to something like below.  It is not clean but it replaces all the non line break \'s with /'s and therefore doesn't insert all the funky symbols in my message box.

public void DisplayAlertBox(string message)
{
    message = message.Replace("\n", "breakN");      
    message = message.Replace("\r", "breakR");
    message = message.Replace("\\", "/");
    message = message.Replace("breakN", "\\n");      
    message = message.Replace("breakR", "\\r");
           
   string popupScript = "<script language='javascript'>" +
   "window.alert(\"" + message +      "\");" +    
   "</script>";              

   Page.RegisterStartupScript(message, popupScript);

}