我有2个HTML输入文本框:“电子邮件”和“密码”。每当我提交这些没有值的文本框时,我都会得到这个SQL异常:SqlException:参数化查询'(@email nvarchar(4000),@password nvarchar(4000))选择电子邮件,Pas'需要参数“@email”,这是没有提供的。我只想要一条消息,说我需要填写电子邮件和密码。我已经有一个JS函数,可以检查这些文本框中的空值,并且它有效。但之后它仍然重定向到“SQL 异常”页。我认为我对数据库属性做了一些愚蠢的事情。我该如何解决这个问题? string sqlquery = "SELECT Email,Password FROM [dbo].[User] WHERE Email = @email and Password = @password;"; SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn); sqlcomm.Parameters.AddWithValue("@email", user.Email); sqlcomm.Parameters.AddWithValue("@password", user.Password); SqlDataReader sdr = sqlcomm.ExecuteReader(); if (sdr.Read()) { return RedirectToAction("Index", "Home"); } else { ViewData["Message"] = "Fout"; } sqlconn.Close(); return View();登录<form asp-action="Login" method="post"> <input id="loginEmail" type="text" name="Email" asp-for="@Model.Email" /> <input id="loginWachtwoord" type="password" name="Password" asp-for="@Model.Password" /> <input type="submit" onclick="CheckNullOrEmpty()" value="Login" /> <h1>@Html.ViewData["Message"]</h1></form>
1 回答

SMILET
TA贡献1796条经验 获得超4个赞
只需在尝试查询数据库之前检查参数
if(string.IsNullOrEmpty(user.Email) || string.IsNullOrEmpty(user.Password)) {
ViewData["Message"] = "Fout";
return View();
}
//... validate first then try to create the statement.
您也可以尝试/捕获错误,如果您不希望弹出错误页面,请在捕获块中返回视图。还有ModelState.IsValid,您可以阅读有关
添加回答
举报
0/150
提交
取消