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

zohair picture zohair · Mar 16, 2009 · Viewed 97.4k times · Source

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,

        try
        {
            do something
        }
        catch 
        {
            messagebox.write("error"); 
            //[This isn't the correct syntax, just what I want to achieve]
        }

[The message box shows the error]

Thank you

Answer

tvanfosson picture tvanfosson · Mar 16, 2009

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.

 try
 {
     ....
 }
 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.