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
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:
You cannot just call messagebox.write cause you are disconnected from the client. You should register javascript code that shows a messagebox:
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:
This will log the technical details of your exception into the system event log (if you need to check the error later) Then 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
Hope his helps. Cheers
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.
What you can do is,
this would show the exception in client side, provided the exception is not bubbled.
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.
If you are using .NET Core with MVC and Razor, you have several levels of preprocessing before your page is rendered. Then I suggest that you try wrapping a conditional error message at the top of your view page, like so:
In ViewController.cs:
In View.cshtml: