Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do see the validation errors on ASP.NET MVC

Posted on 2016-11-20
4
Medium Priority
?
99 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 35

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 35

Expert Comment

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

Featured Post

Industry Leaders: 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

885 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