How to display an error message box in a web application asp.net c#
NickName:zohair Ask DateTime:2009-03-17T02:22:27

How to display an error message box in a web application asp.net 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,

        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

Copyright Notice:Content Author:「zohair」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/651592/how-to-display-an-error-message-box-in-a-web-application-asp-net-c-sharp

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",


More about “How to display an error message box in a web application asp.net c#” related questions

How to display an error message box in a web application asp.net 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, try { do something ...

Show Detail

ASP.NET Web Application Message Box

In an asp.net windows forms application, in the C# code behind you can use: MessageBox.Show("Here is my message"); Is there any equivalent in a asp.net web application? Can I call something from...

Show Detail

message box in asp.net web application

how to display the 'successfully saved' message box in asp.net web application by using javascript. any idea ???

Show Detail

Message box in web application

i am using to Message box alert and it work fine at developing but when deploying and publishing System.Windows.Forms.DialogResult result2 = System.Windows.Forms.MessageBox.Show("msg :\n" + ch...

Show Detail

ASP.NET manage Message Box position with multiple display screens

I have built a web application for users who normally use 2 display monitors. When the application browser is moved to the secondary screen, all message boxes continue to be displayed on the primary

Show Detail

Cannot display message box on dev server in asp.net

I have a code block that is supposed to display a windows style message box to the user in my asp.net application. The user performs an action on an asp.net page, that page then processes data on

Show Detail

Message box in Asp.NET C#

How to display a 1056 character message in message box in asp.NET C#. Response.Write("&lt;script&gt;alert(' " + a + " ')&lt;/script&gt;"); The above code just accepts 54 characters

Show Detail

Message Box show error when the code does not have message box show

I get the following error when I am running my asp.net website from a live site. When I run it locally, I do not get any errors. I do not have anything in the vb.net code side for a message box. I

Show Detail

Displaying a Message in a C# web application

I have a switch statement in C# and I would like to have the default case display an error message when its implemented. I cant use MessageBox because this is a web application but i dont know how...

Show Detail

Display Success Message Box in ASP.NET C#

How can I display messagebox with success logo in ASP.net, I can only display alert message box by using these code. ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Please dont

Show Detail