Solved

How do see the validation errors on ASP.NET MVC

Posted on 2016-11-20
4
22 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 16

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Experts i will try with the Try catch block and let u know
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Thank you karthik !!
1

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now