How to display an error message box in a web application c#


I have an ASP.NET web application, and I wanted to know how I could display an error message box when an exception is thrown.

For example,

            do something
            //[This isn't the correct syntax, just what I want to achieve]

[The message box shows the error]

Thank you

3/16/2009 6:46:40 PM

Accepted Answer

You can't reasonably display a message box either on the client's computer or the server. For the client's computer, you'll want to redirect to an error page with an appropriate error message, perhaps including the exception message and stack trace if you want. On the server, you'll probably want to do some logging, either to the event log or to a log file.

 catch (Exception ex)
     this.Session["exceptionMessage"] = ex.Message;
     Response.Redirect( "ErrorDisplay.aspx" );
     log.Write( ex.Message  + ex.StackTrace );

Note that the "log" above would have to be implemented by you, perhaps using log4net or some other logging utility.

3/16/2009 6:49:16 PM

You cannot just call messagebox.write cause you are disconnected from the client. You should register javascript code that shows a messagebox:

this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow