I am using below code to record error its catching SQL error but its not catching entity error
protected void Application_Error(object sender, EventArgs e)
{
try
{
string constr = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
string sqlStatment = "INSERT INTO ErrorLog VALUES(@Date,@Message)";
///sql error
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlStatment, con))
{
Exception ex = Server.GetLastError().GetBaseException();
if (!(ex is HttpException))
{
string exceptionMessage = ex.Message;
string StackTrace = ex.StackTrace.Replace(Environment.NewLine, string.Empty);
string source = ex.Source.Replace(Environment.NewLine, string.Empty);
string targetSite = ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty);
string userName = HttpContext.Current.User.Identity.Name;
string url = (HttpContext.Current.Request == null || HttpContext.Current.Request.Url == null) ? "" : HttpContext.Current.Request.Url.AbsoluteUri;
string enttiyerror = "";
///entity exception
///
DbEntityValidationException dbEx = new DbEntityValidationException();
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
raise = new InvalidOperationException(message, raise);
enttiyerror = raise.ToString();
}
}
con.Open();
cmd.Parameters.AddWithValue("@Date", DateTime.Now);
cmd.Parameters.AddWithValue("@Message", userName.ToString() + "<br/> <strong>Entity Error </strong> "+ enttiyerror + " <br/> <strong>Exception: </strong> " + exceptionMessage.ToString() + "<br/> <strong>Stack: </strong>" + StackTrace.ToString() + "<br/> <strong>Source: </strong>" + source.ToString() + "<br/> <strong>Target: </strong>" + targetSite.ToString() + "<br/> <strong>URL: </strong>" + url + "<br/> <strong>Name: </strong>" + ((System.Exception)(Server.GetLastError().InnerException)).Message.Trim());
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("~/Error/Error.aspx");
}
}
}
}
catch
{
Response.Redirect("~/Error/Error.aspx");
}
}
for e.g in below code review field max limit it is 100 but i am sending more than 100 char so its given error but global file application error does not show the error details
Entities db = new Entities();
Websitesetup_Testimonial wst = new Websitesetup_Testimonial();
wst.Reviews = "I personally thought it was a really good event. I was pretty excited about all that was going on. The most exciting part of the whole trip for me was, not just the fact that they were learning, but when I was teaching my class in the classroom, everything that I was trying to structure to show my students, they were actually doing in real life action at the facility. I wanted to show the students how to put music together, how to storyboard, how to do lighting, how to be a one stop shop. When we walked into the Esposure building I saw some of Coach T's graphic design students working with graphic designers, then I saw another kid running the camera, another kid was running this, and somebody else was running that, and I was like this was the dream come to life. I was just proud that even some of the lancaster kids were hired or became interns. And the kids who were still in school, that was there touring they were like, Yo Mr. Rich this is playa. But the tour was great and my heart was full!";
db.Websitesetup_Testimonial.Add(wst);
db.SaveChanges();
please advice what code change is needed in global asax so it also catch entity error also