Reputation: 571
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);
}
Upvotes: 1
Views: 4221
Reputation: 419
AddParameterString(command, "@email", email); // Remove this line
command.Parameters.AddWithValue("@email", email); // Add this line
Upvotes: 0
Reputation: 11
The @ symbol is not required when naming a parameter (see: http://www.dotnetperls.com/sqlparameter)
AddParameterString(command, "email", email);
Upvotes: 1