I have this code:
//Update login query
string sql = "ALTER LOGIN " + login.ToUpper() + " WITH PASSWORD = '" + password + "' OLD_PASSWORD = '" + oldpassword + "'";
//Try connection and execute
using (SqlConnection connection = new SqlConnection(GetConnection()))
{
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
command.CommandType = System.Data.CommandType.Text;
var result = command.ExecuteScalar();
connection.Close();
}
This sql query changes the password of a login in database. Notice that it needs the old password to proceed. Altough, if I pass a wrong old password, then it throws a SQLException:
Cannot alter the login 'SEVA', because it does not exist or you do not have permission.
How can I check if the old password is correct before I execute this query, so I can show an error message to the user?