Valerie Meunier

Valerie Meunier

  • 824
  • 693
  • 72.2k

how to catch error message everywhere?

Aug 16 2021 9:45 AM

Hi,

i would like to go back to an unsoved problem till now.

The sql statement is in the aspx file (Website application), not in coode-behind. When an error occurs, i get an ugly message like "cannot insert etc ...."). I can avoid it with this code.

using System;
using System.Web.UI.WebControls;

public partial class leden_upd : System.Web.UI.Page
{

    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
  if (e.Exception == null)
        {
           ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Ok.');", true);
        }
        else
        {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Not ok.');", true);
         e.ExceptionHandled = true;
        }
    }
}

This works. Now i would like to put the code in a static class in order to use this code everywhere in the application.

This a code that i received from someone in C-Corner, but doesn't work (no error but still shows the ugly message)

using System;
using System.Web;
using System.Web.UI;

public partial class ScriptHandler
{
    private Page _page;
    public ScriptHandler(Page myPage)
    {
        _page = myPage;
    }

    public Exception e
    { get; set; }

    public void ShowDialog(bool isError)
    {
        if (!isError)
        {
            ScriptManager.RegisterClientScriptBlock(_page, this.GetType(), "", "alert('ok');", true);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(_page, this.GetType(), "", "alert('not ok');", true);
        }
    }
}
------
 protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
 bool isError = e.Exception == null ? true : false;
        ScriptHandler obj = new ScriptHandler(this.Page);
        obj.ShowDialog(isError);
}

No error, but this shows again the ugly message "Cannot insert the value Null into column etc ...
Thanks

 


Answers (5)