简体   繁体   English

我尝试注册时总是收到此错误

[英]I always get this error when I try to register

I'm using Asp.Net MVC and one of the functions of my program is to register and Login. 我正在使用Asp.Net MVC,我的程序的一个功能是注册和登录。 I always get this error when I try to register. 我尝试注册时总是收到此错误。

System.Data.SqlClient.SqlException: Column name or number of supplied values does not match table definition. System.Data.SqlClient.SqlException:列名或提供的值数与表定义不匹配。

Model: 模型:

public class Users
{
    [Key]
    public int ID { get; set; }

    [Display(Name = "User Type")]
    [Required]
    public int TypeID { get; set; }

    public List<UserType> Types { get; set; }
    public string UserType { get; set; }

    [Display(Name = "Email Address")]
    [MaxLength(80)]
    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Display(Name = "Password")]
    [MaxLength(50)]
    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Display(Name = "First Name")]
    [MaxLength(80)]
    [Required]
    public string FN { get; set; }

    [Display(Name = "Last Name")]
    [MaxLength(80)]
    [Required]
    public string LN { get; set; }


    [Display(Name = "Phone")]
    [MaxLength(12)]
    [MinLength(12)]
    [Required]
    public string Phone { get; set; }


    [Display(Name = "Status")]
    public string CurrentStatus { get; set; }

    [Display(Name = "Status")]
    public List<SelectListItem> Status { get; set; }
}

Controller: 控制器:

  [HttpPost]
    public ActionResult Add(Users record)
    {
        if (IsExisting(record.Email))
        {
            ViewBag.Message = "<div class='alert alert-danger'>Email address already existing.</div>"; 
            record.Types = GetUserTypes();
            return View(record);
        }
        else
        {
            using (SqlConnection con = new SqlConnection(Helper.GetConnection()))
            {
                con.Open();
                string query = @"INSERT INTO users VALUES
                (@typeID, @userEmail, @userPassword, @userFirstName,
                @userLastName, @userPhone, @userStatus)";
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@typeID", record.TypeID);
                    cmd.Parameters.AddWithValue("@userEmail", record.Email);
                    cmd.Parameters.AddWithValue("@userPW", Helper.Hash(record.Password));
                    cmd.Parameters.AddWithValue("@userFN", record.FN);
                    cmd.Parameters.AddWithValue("@userLN", record.LN);
                    cmd.Parameters.AddWithValue("@userPhone", record.Phone);  
                    cmd.Parameters.AddWithValue("@userStatus", "Active");
                    cmd.ExecuteNonQuery();
                    ViewBag.Message = "<div class='alert alert-success'>Record added.</div>"; // displays alert message when record is successfully added
                    ModelState.Clear(); // removes existing user input

                    record.Types = GetUserTypes();
                    return View(record);
                }
            }
        }
    }

If you don't specify columns for users table in an insert statement it expects you to pass values for all the columns 如果未在insert语句中为users表指定列,则需要传递所有列的值

If you don't want to pass values for all the columns simply specify the column name for which you are providing values: 如果您不想为所有列传递值,只需指定要为其提供值的列名称:

INSERT INTO users (typeID, userEmail, userPassword, userFirstName, 
                   userLastName, userPhone, userStatus)
VALUES (@typeID, @userEmail, @userPassword, @userFirstName, 
         @userLastName, @userPhone, @userStatus)

In you query for inserting, before VALUES you need to define the columns... 在查询插入时,在VALUES之前需要定义列...

Like this... 像这样...

string query = "INSERT INTO table (id,username,password,email) VALUES (@id, @username, @password, @email)";

EDIT: 编辑:

string query = "INSERT INTO table (typeID, userEmail, userPW, userFN, userLN, userPhone, userStatus) VALUES (@typeID, @userEmail, @userPW, @userFN, @userLN, @userPhone, @userStatus)"

Also, you must set your primary key to auto increment in the sql database. 此外,您必须在sql数据库中将主键设置为自动增量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我尝试这样做时出现模板错误? - Get a template error when I try to do this? 当我尝试在SQL中导入Excel电子表格时出现错误 - When I try to import an Excel spreadsheet in SQL I get an error 当我尝试在linklabel单击上显示图像时,出现错误 - When I try to show an image on linklabel click, I get an error 尝试使用“ CopyEx”时出现“无效名称”错误 - I get an “Invalid Name” error when I try to use “CopyEx” 为什么在尝试构建时出现错误? - Why do I get an error when I try to build? 尝试连接SQL Server时没有出现错误 - I didn't get error when try to connect SQL Server 当我尝试使用DynamicMethod创建始终返回true的方法时出错 - Getting an error when I try to use DynamicMethod to create a method that always returns true 当我尝试从 mysql 数据库加载图像时,我收到错误“参数无效”,当我尝试从数据库加载图像时 - When I try to load image from mysql database i get error “Parameter is not valid” and when i try to load an image from db 当我尝试将此代码转换为 C 时,我收到一些关于返回类型的错误(我认为) - I get some error about return type(I reckon) when I try convert this code to C 当我尝试保存图像时,出现错误“ GDI +中发生一般错误”。 - When I try to save an image I get the error “A generic error occurred in GDI+.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM