title: Update Database Data in MVC 4
body: <p>Please bear with my noobness, I'm super new to Asp.NET MVC, I don't really understand it yet.</p>
  
  <p>Basically what I want to do is to update a table in my database from information entered into a form. 
  I have a registration form with several options: <code>FirstName</code>, <code>LastName</code>, <code>Age</code>, <code>Sex</code>, <code>SecretQuestion</code>, <code>SecretQuestionAnswer</code>.</p>
  
  <p>I'd like to insert this data in my UserProfile table (I use SimpleMembershipProvider) to the corresponding columns.
  Here's my complete Register ActionResult in my main controller:</p>
  
  <pre><code> [HttpPost]
          [ValidateAntiForgeryToken]
          [RecaptchaControlMvc.CaptchaValidatorAttribute]
          public ActionResult Regisztracio(RegisterModel model, bool captchaValid, string captchaErrorMessage)
          {
              if (ModelState.IsValid)
              {
                  if (!captchaValid)
                  {
                      ModelState.AddModelError("recaptcha", captchaErrorMessage);
                      return View(model);
                  }
  
                  try
                  {
                      OneMillionDb db = new OneMillionDb();
                      User user = new User();
  
                      var token = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, true);
                      user.LastName = model.LastName;
                      user.FirstName = model.FirstName;
                      user.Age = model.Age;
                      user.Sex = model.Sex;
                      user.SecretQuestion = model.SecretQuestion;
                      user.SecretQuestionAnswer = model.SecretQuestionAnswer;
                      db.Users.Add(user);
                      db.SaveChanges();
  
  
  
                          return RedirectToAction("Index");
  
  
                  }
                  catch
                  {
                      ModelState.AddModelError("UserName", "The username already exists.");
                      ModelState.AddModelError("Email", "The specified E-mail address already exists.");
                      ModelState.AddModelError("ConfirmEmail", "");
                      return View(model);
                  }
              }
              return View(model);
          }
  </code></pre>
  
  <p><code>OneMillionDb</code> is my dbContext model file:</p>
  
  <pre><code>public class OneMillionDb : DbContext
      {
          public OneMillionDb()
              : base("DefaultConnection")
          {
          }
          public DbSet&lt;User&gt; Users { get; set; }
      }
  </code></pre>
  
  <p><code>User</code> is my model class for table definiton. It looks like this:</p>
  
  <pre><code>[Table("UserProfile")]
      public class User
      {
          [Key]
          [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
          public int UserId { get; set; }
          [Column]
          [Required]
          public string UserName { get; set; }
          [Column]
          public string FirstName { get; set; }
          [Column]
          public string LastName { get; set; }
          [Column]
          [Required]
          public int Age { get; set; }
          [Column]
          public string Sex { get; set; }
          [Column]
          [Required]
          public string SecretQuestion { get; set; }
          [Column]
          [Required]
          public string SecretQuestionAnswer { get; set; }
          [Column]
          public int MoneyIn { get; set; }
          [Column]
          public int MoneyOut { get; set; }
          [Column]
          public int TimesWon { get; set; }
      }
  </code></pre>
  
  <p>I used to work in ASP.NET Web Pages, in that I'd use the <code>UPDATE</code> SQL statement to do this then execute the query..</p>
  
  <p>Edit3: Here's my Register view:</p>
  
  <pre><code>@model OneMillion.Models.RegisterModel
  @using Recaptcha;
  
  @{
      ViewBag.Title = "Register";
  }
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary()
      &lt;fieldset&gt;
          &lt;legend&gt;Register&lt;/legend&gt;
          &lt;ul&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.UserName)
                  @Html.EditorFor(m =&gt; m.UserName)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.Email)
                  @Html.EditorFor(m =&gt; m.Email)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.ConfirmEmail)
                  @Html.EditorFor(m =&gt; m.ConfirmEmail)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.Password)
                  @Html.PasswordFor(m =&gt; m.Password)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.ConfirmPassword)
                  @Html.PasswordFor(m =&gt; m.ConfirmPassword)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.LastName)
                  @Html.EditorFor(m =&gt; m.LastName)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.FirstName)
                  @Html.EditorFor(m =&gt; m.FirstName)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.Age)
                  @Html.EditorFor(m =&gt; m.Age)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.Sex)
                  @Html.EditorFor(m =&gt; m.Sex)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.SecretQuestion)
                  @Html.EditorFor(m =&gt; m.SecretQuestion)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.SecretQuestionAnswer)
                  @Html.EditorFor(m =&gt; m.SecretQuestionAnswer)
              &lt;/li&gt;
              &lt;li&gt;
                  @Html.Raw(Html.GenerateCaptcha())
              &lt;/li&gt;
          &lt;/ul&gt;
          &lt;input type="submit" value="Register" /&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>And here's my RegisterModel:</p>
  
  <pre><code>public class RegisterModel
      {
          [Required]
          [Display(Name = "Felhasználónév")]
          public string UserName { get; set; }
  
          [Required]
          [DataType(DataType.EmailAddress)]
          [Display(Name = "E-mail cím")]
          public string Email { get; set; }
  
          [DataType(DataType.EmailAddress)]
          [Display(Name = "E-mail cím megerősítése")]
          [Compare("Email", ErrorMessage = "A két e-mail cím nem egyezik.")]
          public string ConfirmEmail { get; set; }
  
          [Required]
          [StringLength(100, ErrorMessage = "A jelszónak legalább {2} karakter hosszúnak kell lennie.", MinimumLength = 6)]
          [DataType(DataType.Password)]
          [Display(Name = "Jelszó")]
          public string Password { get; set; }
  
          [DataType(DataType.Password)]
          [Display(Name = "Jelszó megerősítése")]
          [Compare("Password", ErrorMessage = "A jelszó és a jelszó megerősítése mezők nem egyeznek.")]
          public string ConfirmPassword { get; set; }
  
          [Display(Name= "Vezetéknév")]
          public string LastName { get; set; }
  
          [Display(Name= "Keresztnév")]
          public string FirstName { get; set; }
  
          [Display(Name= "Életkor")]
          public int Age { get; set; }
  
          [Display(Name="Nem")]
          public string Sex { get; set; }
  
          [Required]
          [Display(Name="Titkos kérdés")]
          public string SecretQuestion { get; set; }
  
          [Required]
          [Display(Name="Titkos kérdés válasza")]
          public string SecretQuestionAnswer { get; set; }
      }
  </code></pre>
  
  <p>Edit:
  <strong>My Problem:</strong></p>
  
  <p>The database doesn't get updated. I know this since I get an Exception stating that Column Age can't have a value of <code>NULL</code>, but the corresponding input was filled out.</p>
  
  <p>Edit2:
  I also changed the code to reflect <strong>SOfanatic</strong>'s answer, but I still get the error.</p>
  
  <p>Edit4: Here's the exception I get in the Catch side of trycatch:</p>
  
  <pre><code>Cannot insert the value NULL into column 'Age', table 'OneMillionDb.dbo.UserProfile'; column does not allow nulls. INSERT fails.
  </code></pre>