Search code examples
asp.net-mvc-4simplemembership

System.Data.SqlClient.SqlException: Invalid column name 'Email'


I'm working on a new ASP.Net MVC 4 app and testing user login. I am getting following exception:

System.Data.SqlClient.SqlException: Invalid column name 'Email'

I'm using the example from http://kevin-junghans.blogspot.com/2013/02/adding-email-confirmation-to.html.

Here's the appropriate code:

[AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    string confirmationToken =
                        WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email }, true);

                    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("noreply@cardmage.com");
                    System.Net.Mail.MailAddress[] to = new System.Net.Mail.MailAddress[1];
                    to[1] = new System.Net.Mail.MailAddress(model.Email);
                    System.Net.Mail.MailAddress[] cc = new System.Net.Mail.MailAddress[0];
                    System.Net.Mail.MailAddress[] bcc = new System.Net.Mail.MailAddress[0];
                    String Subject = "Please verify your e-mail address.";
                    String body = String.Format("Thank you for registering with CardMage." +
                        "To verify your account, please follow this link." +
                        "http://www.cardmage.co/Account/RegisterConfirmation/{0}", confirmationToken);
                    System.Net.NetworkCredential nc = new NetworkCredential("foo", "bar");
                    SendGrid sg = SendGrid.GetInstance(from, to, cc, bcc, Subject, body, "");
                    SMTP s = SMTP.GetInstance(nc);
                    s.Deliver(sg);
                    return RedirectToAction("RegisterStepTwo", "Account");

What am I doing wrong? I tried to find the solution from from the google search but no luck. I found this SO question that has the property values formatted the same way I do, so I checked my database, but the Email column does not exist. Can someone please point me in the right direction? Thank you for your time and consideration.


Solution

  • To get your email field updated by the call to CreateUserAndAccount, do the following:

    1 Ensure the database table you are using has an email column.

    2 Change the Account model UserProfile to include your email field.

    3 Change the Register view to include your email.

    4 Change the AccountController to include an anomymous object (this you have done)

    Then, when you Register a new user, their email will be stored in the database table.

    This works for me.