Answers
tvanfosson 2009-03-16T18:25:27
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.\n\n try\n {\n ....\n }\n catch (Exception ex)\n {\n this.Session[\"exceptionMessage\"] = ex.Message;\n Response.Redirect( \"ErrorDisplay.aspx\" );\n log.Write( ex.Message + ex.StackTrace );\n }\n\n\nNote that the \"log\" above would have to be implemented by you, perhaps using log4net or some other logging utility.",
Michiel Overeem 2009-03-16T18:26:39
You cannot just call messagebox.write cause you are disconnected from the client. You should register javascript code that shows a messagebox:\n\nthis.RegisterClientScriptBlock(typeof(string), \"key\", string.Format(\"alert('{0}');\", ex.Message), true);\n",
Ramesh 2009-03-16T18:28:50
using MessageBox.Show() would cause a message box to show in the server and stop the thread from processing further request unless the box is closed.\n\nWhat you can do is, \n\nthis.Page.ClientScript.RegisterStartupScript(this.GetType(),\"ex\",\"alert('\" + ex.Message + \"');\", true);\n\n\nthis would show the exception in client side, provided the exception is not bubbled.",
Perchik 2009-03-16T18:27:05
The way I've done this in the past is to populate something on the page with information when an exception is thrown. MessageBox is for windows forms and cannot be used for web forms. I suppose you could put some javascript on the page to do an alert:\n\nResponse.Write(\"<script>alert('Exception: ')</script>\");\n",
Jeremy Cron 2009-03-16T18:28:27
I wouldn't think that you would want to show the details of the exception. We had to stop doing this because one of our clients didn't want their users seeing everything that was available in the exception detail. Try displaying a javascript window with some information in it explaining that there has been a problem.",
Jaime 2011-06-26T05:47:51
If you want to handle all your error on a single place, you can use the global.asax file (also known as global application file) of your webapplication, and work with the application error event. It goes like this Firts you add the global application file to your project, then on the Application_Error event you put some error handling code, like this:\n\n\n\n void Application_Error(object sender, EventArgs e) \n{\n Exception objErr = Server.GetLastError().GetBaseException();\n string err = \"Error Caught in Application_Error event\\n\" +\n \"Error in: \" + Request.Url.ToString() +\n \"\\nError Message:\" + objErr.Message.ToString() +\n \"\\nStack Trace:\" + objErr.StackTrace.ToString();\n System.Diagnostics.EventLog.WriteEntry(\"Sample_WebApp\", err, System.Diagnostics.EventLogEntryType.Error);\n Server.ClearError();\n Response.Redirect(string.Format(\"{0}?exceptionMessage={1}\", System.Web.VirtualPathUtility.ToAbsolute(\"~/ErrorPage.aspx\"), objErr.Message));\n}\n\n\nThis will log the technical details of your exception into the system event log (if you need to check the error later)\nThen on your ErrorPage.aspx you capture the exception message from the querystring on the Page_Load event. How to display it is up to you (you can use the javascript alert suggested on the other answers or simple pass the text to a asp.net literal\n\nHope his helps. Cheers",