I have two more methods the same as this except that they query a different table. Is there another way to know if the result of the query is null
? The hasher.CompareStringToHash()
method is not allowed to have a null
value.
If it's not null
, it proceeds to an else
condition, and inside that it checks if the encrypted password and password in the textfield are the same. If it's the same it will go to another else
statement.
Is there another way to shorten this code? Keep in mind that I should not have a null
value in the hasher.CompareStringToHash()
. Or, if possible, restrict calling that method if the result is null
and lessen the if
statements. How can I make this code cleaner and more efficient?
public void CheckAssistantsPassword()
{
DbClassesDataContext myDb = new DbClassesDataContext(dbPath);
var password = (from userAccounts in myDb.Assistants
where userAccounts.Ass_UserName== txtUserName.Text
select userAccounts.Ass_Password).FirstOrDefault();
var hasher = new Hasher() { SaltSize = 16 };
if (password == null)
{
MessageBox.Show("Invalid Account");
}
else
{
bool isOkay = hasher.CompareStringToHash(txtPassword.Text,password);
if (isOkay)
{
MessageBox.Show("You May Now Login");
}
else
{
MessageBox.Show("INVALID PASSWORD");
}
}
}