gpt4 book ai didi

c# - 我尝试注册时总是遇到此错误

转载 作者:行者123 更新时间:2023-11-30 15:55:54 25 4
gpt4 key购买 nike

我正在使用 Asp.Net MVC,我的程序的功能之一是注册和登录。我尝试注册时总是遇到此错误。

System.Data.SqlClient.SqlException: Column name or number of supplied values does not match table definition.

型号:

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);
}
}
}
}

最佳答案

如果您没有在插入语句中为用户表指定列,它希望您为所有列传递值

如果您不想为所有列传递值,只需指定您为其提供值的列名:

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

关于c# - 我尝试注册时总是遇到此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47791560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com