Asp.net 未提供存储过程参数
这是我的模型类,与数据库中的表相同:Asp.net 未提供存储过程参数,asp.net,sql-server,asp.net-mvc,stored-procedures,parameter-passing,Asp.net,Sql Server,Asp.net Mvc,Stored Procedures,Parameter Passing,这是我的模型类,与数据库中的表相同: public class AttendanceModel { public int UserId { get; set; } [Display(Name = "Login As")] public string UserRole { get; set; } public string Password { get; set; } public bool isA
public class AttendanceModel
{
public int UserId { get; set; }
[Display(Name = "Login As")]
public string UserRole { get; set; }
public string Password { get; set; }
public bool isActive { get; set; }
public string Email { get; set; }
public int Mobile { get; set; }
}
这是我的视图文件。如果我有任何错误,请告诉我:
@model AttendanceManagement.Models.AttendanceModel
@{
ViewBag.Title = "LoginForm";
Layout = null;
}
<!DOCTYPE html>
<head>
<title>Login</title>
<!-- meta tags -->
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="keywords" content="Art Sign Up Form Responsive Widget, Audio and Video players, Login Form Web Template, Flat Pricing Tables, Flat Drop-Downs, Sign-Up Web Templates,
Flat Web Templates, Login Sign-up Responsive Web Template, Smartphone Compatible Web Template, Free Web Designs for Nokia, Samsung, LG, Sony Ericsson, Motorola Web Design" />
<!-- /meta tags -->
<!-- custom style sheet -->
<link href="~/LoginStyles/css/style.css" rel="stylesheet" />
<!-- /custom style sheet -->
<!-- fontawesome css -->
<link href="~/LoginStyles/css/fontawesome-all.css" rel="stylesheet" />
<!-- /fontawesome css -->
<!-- google fonts-->
<link href="//fonts.googleapis.com/css?family=Raleway:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i"
rel="stylesheet">
<!-- /google fonts-->
</head>
@using (Html.BeginForm())
{
<body>
<h1>Register</h1>
<div class="w3l-login-form">
<h2>Register Here</h2>
<div class="w3l-form-group">
<label>Usertype:</label>
<div class="group">
<select id="txtUser" class="form-control">
<option>Admin</option>
<option>Teacher</option>
<option>Student</option>
</select>
</div>
</div>
<div class="w3l-form-group">
<label>Email:</label>
<div class="group">
<i class="fas fa-envelope"></i>
<input type="email" class="form-control" placeholder="Email" required="required" id="txtEmail" />
</div>
</div>
<div class="w3l-form-group">
<label>Password:</label>
<div class="group">
<i class="fas fa-unlock"></i>
<input type="password" class="form-control" placeholder="Password" required="required" id="txtPassword" />
</div>
</div>
<div class="w3l-form-group">
<label>Mobile:</label>
<div class="group">
<i class="fas fa-phone"></i>
<input type="number" class="form-control" placeholder="Mobile No" required="required" id="txtMobile" />
</div>
</div>
<input type="button" id="btnRegister" value="register" onclick="Register()" />
</div>
</body>
}
@*@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}*@
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script language="javascript">
function Register() {
$("#btnRegister").val('Please wait..');
$.ajax({
url: "@Url.Content("~/Attendance/Register")",
data: { Usertype: $("#txtUser").val(), Email: $("#txtEmail").val(), Password: $("#txtPassword").val(), Mobile: $("#txtMobile").val() },
cache: false,
type: "POST",
success: function (data) {
if (data.StatusCode == 'OK') {
alert("Successfull login.");
window.location.href = '@Url.Content("~/Attendance/LoginForm")';
} else {
alert(data.Message);
}
},
error: function (reponse) {
alert("error : " + reponse);
}
});
$("#btnRegister").val('register');
}
</script>
这是我的存储过程代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[attendance_insert]
@pUserRole varchar(10),
@pEmail varchar(50),
@pPassword varchar(50),
@pMobile varchar(50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO AttendanceLogin (UserRole, Email, Password, Mobile, isActive)
VALUES (@pUserRole, @pEmail, @pPassword, @pMobile, 1)
END
请帮助我注意错误,因为我总是重复这个错误。您犯了两个错误,这就是为什么您得到“未提供参数” 第一名: 在此类
AttendanceModel
中,用户ID未设置为主键。因此,存储过程期望它的值
秒:
在控制器中,您需要UserId
这里:
UserId = Convert.ToInt32(dr["UserId"].ToString())
这显然会导致错误,因为它无法找到UserId
,并且您没有将其传递到存储过程中
您可以做的是添加一个[Key]
数据注释,使其成为主键。这将在该字段上自动生成主键,您将消除此错误:
public class AttendanceModel
{
[Key] // <-- Add this. You need to import using System.ComponentModel.DataAnnotations
public int UserId { get; set; }
[Display(Name = "Login As")]
public string UserRole { get; set; }
public string Password { get; set; }
public bool isActive { get; set; }
public string Email { get; set; }
public int Mobile { get; set; }
}
请添加您获得的完整且准确的错误消息。添加参数时,您缺少@符号。例如,您需要
cmd.Parameters.AddWithValue(“@pUserRole”,SqlDbType.VarChar)。Value=register.UserRole
@JonathanWillock,它还没有解决我的问题。请尝试将AddWithValue
替换为普通的Add
-AddWithValue期望值作为第二个参数,而不是存储过程不选择任何内容的类型,因此,不需要填充数据集-使用cmd.ExecuteNonQuery(并删除DataAdapter)您可能是对的(在这种具体情况下,可能性很大)-但我总是觉得有点难以猜测错误可能是什么-OP的工作应该是向我们展示完整的错误,这样我们就知道错误是什么,以及建议如何修复。我也尝试过在模型中添加关键注释,但错误仍然是一样的。
public class AttendanceModel
{
[Key] // <-- Add this. You need to import using System.ComponentModel.DataAnnotations
public int UserId { get; set; }
[Display(Name = "Login As")]
public string UserRole { get; set; }
public string Password { get; set; }
public bool isActive { get; set; }
public string Email { get; set; }
public int Mobile { get; set; }
}
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pUserRole", SqlDbType.VarChar).Value = register.UserRole;
cmd.Parameters.AddWithValue("@pEmail", SqlDbType.VarChar).Value = register.Email;
cmd.Parameters.AddWithValue("@pPassword", SqlDbType.VarChar).Value = register.Password;
cmd.Parameters.AddWithValue("@pMobile", SqlDbType.VarChar).Value = register.Mobile;
da.SelectCommand = cmd;
con.Open();
da.Fill(dt);
}