Hello,
i am using this global.asax file to catch the errors in my application
i have the field "Price" in database which take decimal user did not type any value in textbox and save the data when insert run so it gives errors input string not in correct format but the error does not give information which input field has not valid value i have more than 100 fields on form and it is difficult to find at time.
Please advice how to get actual field name which is giving error.
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 = "";
if (ex is DbEntityValidationException)
{
var typedEx = ex as DbEntityValidationException;
//Now poke typedEx to get through root cause of your failure
//Exception raise = EntityDataSourceValidationException
foreach (var validationErrors in typedEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
enttiyerror = 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);
}
}
}
con.Open();
cmd.Parameters.AddWithValue("@Date", DateTime.Now);
cmd.Parameters.AddWithValue("@Message","UserName " + 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");
}