Search code examples
c#asp.netsql-serverauthenticationalter

Change MS SQL Login Password in ASP.NET


I'm trying to change passwords of SQL server logins, but the Passwords just won't change. This is what I have:

SqlCommand query = new SqlCommand("ALTER LOGIN @login WITH PASSWORD = '@Pwd'", con);
query.Parameters.Add("@login", SqlDbType.NVarChar, 60).Value = login;
query.Parameters.Add("@Pwd", SqlDbType.NVarChar, 20).Value = _newPwds[login];
query.Prepare();
query.BeginExecuteNonQuery();

I don't get any error messages back from the SQL Server or debugging (Or I don't know how to get them). The Connection is correct and I have the rights to Change Passwords on the Server (Application is running with Integrated Security).

Thanks for your help.


Solution

  • You are using an asynchronous method BeginExecuteNonQuery to execute the query, so you haven't captured it. Given you probably didn't want to go async, try using ExecuteNonQuery instead:

    query.ExecuteNonQuery();
    

    Plus you can't use parameters in an ALTER LOGIN statement, see this question.