私はAsp.Net MVCを使用しています。私のプログラムの機能の1つは、登録してログインすることです。私は登録しようとするといつもこのエラーが出ます。登録しようとするといつもこのエラーが出る
System.Data.SqlClient.SqlException:指定された列名または数値の 値がテーブル定義と一致しません。
モデル:
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; }
}
はコントローラー:
[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);
}
}
}
}
あなたは '@ userPassword'と' @ userPW'の両方を持ち、他の値と同様です。パラメータ名を1つ選んでください! – Andrei