I'm building a login page using C# forms for my app.
So far, I have implemented how to insert a username, with a specific username and hashed password.
Now in order to retrieve it, i'm using this function.
Since the username value is unique, when i run my sql statement i will only receive 1 element, which is the password for this username if it is available as a string.
But when i have string pass = cmd.ExecuteNonQuery(); i get an error in my code. Is there any way to solve this without having to create a datareader ?
private void btn_Login_Click(object sender, EventArgs e)
{
try
{
sc.Open();
SqlCommand cmd = new SqlCommand("SELECT Password FROM dbo.Login WHERE username=@UN", sc);
cmd.Parameters.AddWithValue("@UN", tb_Username);
string pass = cmd.ExecuteNonQuery();
if (Decrypt(pass) == tb_Password)
{
Find_Resource show_now = new Find_Resource();
show_now.Show();
this.Hide();
}
else MessageBox.Show("Incorrect credentials");
sc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}