1

I get this error even if I checked and I do have this parameter in my database:

The parameterized query '(@UserName nvarchar(4000))SELECT Email AS Email FROM AspNetUsers' expects the parameter '@UserName', which was not supplied.

This is in my 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");
}

I will be grateful for any help.

  • It seems you are passing `NULL` value to the `@UserName` param (your `EConfuser` value is null). Refer this https://stackoverflow.com/a/8530911/311255 – SelvaS Jun 17 '20 at 22:54

1 Answers1

0

Try this:

cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = EConfUser ?? 
(object)DBNull.Value;
Nonik
  • 645
  • 4
  • 11