1

During execution of this code reader shows "Enumeration yielded no results" and the method return empty model. I have no idea what's wrong

 public UserModel GetUser(string email)
    {
        email = email.ToString();
        var connection = OpenConnection();
        var command = connection.CreateCommand();
        command.CommandText = "select * from Users where UserName = @email;";
        AddParameterString(command, "@email", email);
        SqlDataReader reader = command.ExecuteReader();
        UserModel model = new UserModel();
        while (reader.Read())
        {
            model.ConfirmedEmail = Convert.ToBoolean(reader["ConfirmedEmail"]);
            model.UserId = int.Parse(reader["userId"].ToString());
            model.UserName = reader["UserName"].ToString();
            model.UserEmail = reader["UserEmail"].ToString();
            model.PasswordHash = reader["PasswordHash"].ToString();
            model.PasswordSalt = reader["PasswordSalt"].ToString();
            model.UserRole = reader["UserRole"].ToString();
        }
        return model;
    }
}



 protected void AddParameterString(SqlCommand command, string parameterName, string value)
    {
        var newParameter = command.CreateParameter();
        newParameter.ParameterName = parameterName;
        newParameter.DbType = System.Data.DbType.String;
        newParameter.Value = value;
        command.Parameters.Add(newParameter);
    }
6
  • List field names explicitly in select statement. Chances are that some name is mistyped. Commented Jul 24, 2016 at 17:32
  • @AlexKudryashev Enumeration yielded no results means that the reader is empty. Commented Jul 24, 2016 at 18:09
  • while(reader.Read()) means that OP enter the inner block and at least one row exists. Commented Jul 24, 2016 at 18:11
  • @AlexKudryashev The OP never stated that the while block got executed. Commented Jul 24, 2016 at 19:28
  • Most probably the error comes from Convert.ToBoolean(reader["ConfirmedEmail"]);. See stackoverflow.com/questions/30600370/… Commented Jul 24, 2016 at 19:46

2 Answers 2

1

The @ symbol is not required when naming a parameter (see: http://www.dotnetperls.com/sqlparameter)

AddParameterString(command, "email", email);
Sign up to request clarification or add additional context in comments.

Comments

0

AddParameterString(command, "@email", email); // Remove this line

command.Parameters.AddWithValue("@email", email); // Add this line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.