0

I have a register form that when a user registers, his/her information gets saved to the database. With the code I have I am getting this

An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in login.dll but was not handled in user code

Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

error at the last line. When I checked what gets saved into the variables, everything is null except what is acquired as input from the form.

Ex. userType = null, IPAddress = null, CreatedDate = 1/1/0001 12:00:00 AM, but username, email, etc. have the correct values from the form.

public ActionResult Register(Models.User user)
{
    try
    {
        if (ModelState.IsValid)
        {
            using (var db = new login.Models.ShareNotesEntities())
            {
                var crypto = new SimpleCrypto.PBKDF2();
                var encrypPass = crypto.Compute(user.Password);
                var newUser = db.Users.Create();

                newUser.Username = user.Username;
                newUser.Email = user.Email;
                newUser.Password = encrypPass;
                newUser.PasswordSalt = crypto.Salt;
                newUser.Grade = user.Grade;
                newUser.UserType = "User";
                newUser.CreatedDate = DateTime.Now;
                newUser.IsActive = true;
                newUser.IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ??Request.ServerVariables["REMOTE_ADDR"];

                db.Users.Add(newUser);
                db.SaveChanges();

                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "sdfasdf");
        }
    }
    catch (DbEntityValidationException e)
    {
        foreach (var eve in e.EntityValidationErrors)
        {
            Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
            foreach (var ve in eve.ValidationErrors)
            {
                Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
            }
        }
        throw;
    }
    return View();
} -----> ERROR HERE
ekad
  • 14,436
  • 26
  • 44
  • 46
User765876
  • 135
  • 2
  • 11
  • Try some Debugging: add breakpoint to the line: catch (DbEntityValidationException e) and run F5. Also try to apply the answer: http://stackoverflow.com/a/22638624/2048391 Hopefully it helps :-) – jyrkim Feb 15 '15 at 18:16
  • First thing to check: is `Models.User user` OK? Is `newUser` just a simple POCO? – Gert Arnold Feb 15 '15 at 20:37
  • Stupid question, but are you actually expanding the EntityValidationErrors collection on the Exception to see what it can tell you? – pwdst Feb 16 '15 at 09:35
  • Your `throw` at the end of your `catch` block is rethrowing the exception you're trying to unroll and print to Console: chances are, the detailed report you've created walking `DbEntityValidationException.EntityValidationErrors` is back there somewhere, but the original exception popping in you face is distracting you from it. Try removing the `throw`, and see where that gets you. Also, either put in a `Console.ReadKey()` or a breakpoint somewhere near the end of that block, so you can actually read the output. – John Castleman Feb 17 '15 at 05:25

0 Answers0