asp.net-mvc-5 - 使用 (SqlDataReader reader = cmd.ExecuteReader()) 错误 asp.net mvc5
问题描述
即使我检查过并且我的数据库中确实有此参数,我也会收到此错误:
参数化查询“(@UserName nvarchar(4000))SELECT Email AS Email FROM AspNetUsers”需要参数“@UserName”,但未提供该参数。
这是在我的 AccountController 中:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SendConfirmationMail() // resend confirmation link in case the users haven't received it
{
string res = null;
string connection = GetConnectionString("DefaultConnection");
using (SqlConnection myConnection = new SqlConnection(connection))
{
myConnection.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT Email AS Email FROM AspNetUsers WHERE UserName = @UserName";
cmd.Connection = myConnection;
cmd.Parameters.AddWithValue("@UserName", EConfUser);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
res = reader["Email"].ToString(); // get user's email from db
var user = await UserManager.FindByEmailAsync(res); // find applicationuser user by email
//update user's email link date
UpdateEmailLinkDate(EConfUser);
// generate email conf token and send it
codeType = "EmailConfirmation";
await SendEmail("ConfirmEmail", "Account", user, res, "WelcomeEmail", "Confirm your account");
}
myConnection.Close();
}
}
}
return RedirectToAction("ConfirmationEmailSent", "Account");
}
我将不胜感激任何帮助。
解决方案
尝试这个:
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = EConfUser ??
(object)DBNull.Value;
推荐阅读
- matplotlib - Matplotlib 年绘制为年末
- javascript - 尽管导入了模块并包含 CUSTOM_ELEMENTS_SCHEMA 模式,但组件不是已知元素
- sql - 需要返回空字符串而不是 0
- python-3.x - 如何使用 pyaudio 播放 wav 二进制数据?
- android - 如何使我的 /data 文件夹从 RO 到 RW?
- excel - 将 VBA 变量条件存储在单元格中
- java - 在 java maven 项目上使用 GraphFames
- java - 自动调整大小和自动换行 JToolbar
- design-patterns - Alfresco的微服务如何访问数据库?
- python - Scrapy:检查页面是否包含 HTML 表单元素