Ramco Ramco

Ramco Ramco

  • 440
  • 3.4k
  • 529.5k

Error Message not displayed

Jul 8 2021 3:41 PM

Hi

I may be repeating this question. i deliberately changed Stored Procedure Name in class GetAllLocation. I am able to Log error message in Database.

But i wamt that some message should also get displayed to user. With this code it is not displaying any message to User.

Global.asax

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();
    if (exception != null)
    {
        Response.Clear();
        HttpException httpException = exception as HttpException;
        if (httpException != null)
        {
            string action = "Index";
            // clear error on server 
            Response.Clear();
            Server.ClearError();
            Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
        }
    }
}
public class ErrorController : Controller
{
    public ActionResult Index(string message)
    {
        ViewBag.ErrorMessage = message;
        return View();
    }
}

Error Index

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <h3>@ViewBag.ErrorMessage</h3>
</body>
</html>

Below is the Class

public List<Location> GetAllLocation()
{
    List<Location> objLocation = new List<Location>();
    try
    {
        using (SqlConnection con = new SqlConnection(Common.Con))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_Location1", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Action", "R");
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                objLocation.Add(new Location
                {
                    Id = rdr["Id"].ToString(),
                    Description = rdr["Description"].ToString(),
                    IsActive = Convert.ToBoolean(rdr["IsActive"]),
                });
            }
        }
    }
    catch (Exception ex)
    {
        ExceptionLogging.SendExcepToDB(ex);
    }
    return objLocation;
}

Thanks


Answers (5)