?
Solved

How do see the validation errors on ASP.NET MVC

Posted on 2016-11-20
4
Medium Priority
?
92 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 30

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 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 1000 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 30

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

719 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