Hi
Error Handling in ASP.Net MVC
I deliberately changed Stored Procedure Name. I am able to Log error message in Database. But i want that some message should also get displayed to user.
With this code it is not displaying any message to User.
View
@ViewBag.ErrorMessage
Controller
public class ErrorController : Controller
{
public ActionResult Index(string message)
{
ViewBag.ErrorMessage = message;
return View();
}
}
Global.asax
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
string action;
switch (httpException.GetHttpCode())
{
case 404:
// page not found
action = "HttpError404";
break;
case 500:
// server error
action = "HttpError500";
break;
default:
action = "General";
break;
}
// clear error on server
Server.ClearError();
Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
}
}
Below is the Class where error is comning
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