6

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

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

Community
  • 1
  • 1
zohair
  • 2,369
  • 10
  • 35
  • 41
  • Duplicate of http://stackoverflow.com/questions/651592/how-to-display-an-error-message-box-in-a-web-application-asp-net-c/ – Perchik Mar 16 '09 at 19:03
  • If you edit the question to link to a duplicate, please put the link at the bottom so it doesn't alter the summary text on the main pages. – Joel Coehoorn Mar 16 '09 at 19:06
  • @ZOHAIR: You can see your previous questions and their answers at http://stackoverflow.com/users/70398/zohair – Ramesh Mar 16 '09 at 19:06
  • ... this is one time when 'Exact Duplicate' isn't going to get any argument. – John MacIntyre Mar 16 '09 at 19:06
  • That was the point of putting it at the top so people would recognize it as a duplicate and be able to close it more easily. – tvanfosson Mar 16 '09 at 19:07
  • ...but I'm not going to fuss about it. If you want it at the bottom, so be it. – tvanfosson Mar 16 '09 at 19:08
  • @tvanfosson: Now that I see the whole thing, you were probably right in this instance. I didn't realize this was an EXACT (emphasis) duplicate, down the same user. For most other duplicates, I think breaking their preview text should be considered 'rude'. – Joel Coehoorn Mar 16 '09 at 19:13
  • @Joel -- understood, though I would tend to favor not wasting the time of people who expend effort answering a question that's already been asked (and answered) even if the text is not exactly the same as in this case. – tvanfosson Mar 16 '09 at 19:16

3 Answers3

8

Roughly you can do it like that :

try
{
    //do something
}
catch (Exception ex)
{
    string script = "<script>alert('" + ex.Message + "');</script>";
    if (!Page.IsStartupScriptRegistered("myErrorScript"))
    {
         Page.ClientScript.RegisterStartupScript("myErrorScript", script);
    }
}

But I recommend you to define your custom Exception and throw it anywhere you need. At your page catch this custom exception and register your message box script.

Canavar
  • 47,715
  • 17
  • 91
  • 122
4

The errors in ASP.Net are saved on the Server.GetLastError property,

Or i would put a label on the asp.net page for displaying the error.

try
{
    do something
}
catch (YourException ex)
{
    errorLabel.Text = ex.Message;
    errorLabel.Visible = true;
}
Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
3

All you need is a control that you can set the text of, and an UpdatePanel if the exception occurs during a postback.

If occurs during a postback: markup:

<ajax:UpdatePanel id="ErrorUpdatePanel" runat="server" UpdateMode="Coditional">
<ContentTemplate>
<asp:TextBox id="ErrorTextBox" runat="server" />
</ContentTemplate>
</ajax:UpdatePanel>

code:

try
{
do something
}

catch(YourException ex)
{
this.ErrorTextBox.Text = ex.Message;
this.ErrorUpdatePanel.Update();
}
MStodd
  • 4,716
  • 3
  • 30
  • 50