public class UserLoginInfo
{
public UserRole Role;
public string Username;
public static UserLoginInfo FetchUser(string username, string password)
{
using (var connection = Utils.Database.GetConnection())
using (var command = new SqlCommand("SELECT [Username], [Password], [Role] FROM [Users] WHERE [Username] = @username", connection))
{
command.Parameters.AddWithValue("@username", username);
using (var reader = command.ExecuteReader())
{
if (reader == null || !reader.Read() || !Utils.Hash.CheckPassword(username, password, (byte[])reader["Password"]))
throw new Exception("Wrong username or password.");
return new UserLoginInfo { Username = (string)reader["Username"], Role = (UserRole)reader["Role"] };
}
}
}
}
When I put a breakpoint and debug the error comes from this line
return
new UserLoginInfo
{
Username = (string)reader["Username"],
Role = (UserRole)reader["Role"]
};
I don't understand why I get this error. Please help me!
EDIT: How can I convert (string)reader["Role"] to UserRole??
public enum UserRole
{
Admin,
Maintance,
User
}