Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid column name 'Length' edited MVC 4 Membership

I am trying to add to the MVC 4 out of the box membership.

public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        try
        {
            WebSecurity.CreateUserAndAccount(model.UserName, model.Password, model.Mobile);
            //I added model.Mobile
            WebSecurity.Login(model.UserName, model.Password);
            return RedirectToAction("Index", "Home");
        }
        catch (MembershipCreateUserException e)
        {
            ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
        }
    }
}

I changed the model

public class RegisterModel
{
    // Username and password methods

    public string Mobile { get; set; }
}

I also added it to the UserProfile Model. I am getting a SqlException and i just don't see the connection. It seems like such a simple error but. The DB is set to Nvarchar(MAX) which is to try to avoid this error.

Invalid column name 'Length' 
like image 431
jackncoke Avatar asked Dec 12 '25 13:12

jackncoke


1 Answers

It's actually throwing the error because it expects a dictionary of additional properties - try:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new{model.Mobile}, false)

This assumes that you have a column called Mobile in the UserProfileTable. If you wanted to specify a different column name:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new{YourColumnName = model.Mobile}, false)
like image 143
El Kabong Avatar answered Dec 15 '25 10:12

El Kabong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!