Search code examples
c#asp.net-mvcjqgrid

Error: Validation failed for one or more entities (C# MVC)


I'm trying to get my JqGrid to update my database. I keep getting an Error: "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

how do I find out what my actual error is or whats causing the error?

 // TODO:insert a new row to the grid logic here  
    [HttpPost]
    public string Create([Bind(Exclude = "Id")] AspNetUser obj)
    {
        //System.Diagnostics.Debug.WriteLine("Create");
        string msg;
        try
        {
            if (ModelState.IsValid)
            {
                db.AspNetUsers.Add(obj);
                //db.AspNetUsers.Add(new AspNetUser { UserName = obj.UserName, Email = obj.Email });
                db.SaveChanges();
                msg = "Saved Successfully";
            }
            else
            {
                msg = "Validation data not successfull";
            }
        }
        catch (Exception ex)
        {
            msg = "Error occured:" + ex.Message;
        }
        return msg;
    }

Solution

  • Usually this exception means the database is failing validation. This can occur if you have a set field in the database with a set character count and you are trying to save to that field a string which exceeds the database count.

    Update your database string types with a 'varchar(max)' for example.

    Hope this helps