Solved

How do see the validation errors on ASP.NET MVC

Posted on 2016-11-20
4
53 Views
Last Modified: 2016-11-22
Hi Experts ,

When am trying to insert the data via Entity framework . I got an error as
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
. where do I check what are the validation errors are there
0
Comment
Question by:karthik80c
  • 2
4 Comments
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 total points
ID: 41895215
Do you have try catch block in your code.?

Always please put your code in try catch block. <<My fav>>
try
{
    ---- Your code here !!
}
Catch(DbEntityValidationException dbEx)
{
    foreach (var validationErrors in dbEx.EntityValidationErrors)
      {
         foreach (var validationError in validationErrors.ValidationErrors)
           {
              Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
           }
      }
}

Open in new window


<<The above code is from - http://www.technologycrowds.com/2015/05/entity-framework-error-validation-failed-for-one-or-more-entities.html>>

Hope it helps !!
1
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 250 total points
ID: 41895457
Try below code and you will get an exact error

public ActionResult Create(EmpRegistration collection)  
{  
    try  
    {  
        if (ModelState.IsValid)  
        {  
            EmpRegistration empRegis = new EmpRegistration();  
            // TODO: Add insert logic here  
            empRegis.Address = collection.Address;  
            empRegis.City = collection.City;  
            empRegis.Id = 7;  
            empRegis.Name = collection.Name;  
            objEnity.EmpRegistrations.Add(empRegis);  
            objEnity.SaveChanges();  
  
            return View();  
        }  
        return View(objEnity.EmpRegistrations);  
    }  
    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)  
    {  
        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);  
            }  
        }  
        throw raise;  
    }  
}  

Open in new window

1
 

Author Comment

by:karthik80c
ID: 41895490
Thanks Experts i will try with the Try catch block and let u know
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41895493
Thank you karthik !!
1

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question