4

When you are using a SqlConnection to connect to a MS Sql Server, if the password has expired you will get a SqlException Number: 18487 or 18488.

How can you change the user's password in code during the connection attempt?

1 Answer 1

4

Use the static SqlConnection.ChangePassword() method.

string original_dsn = "server=mysql.server.com,1433;database=pubdb;User Id={0};Password={1};"
string dsn = String.Format(original_dsn, username, password);

SqlConnection conn = new SqlConnection( dsn );
try
{
     conn.Open();
}
catch(SqlException e)
{
    if (e.Number == 18487 || e.Number == 18488)
           SqlConnection.ChangePassword(dsn, newpassword);
           // Try login again here with new password
    else
           MessageBox.Show(e.Message);
}
finally 
{
    conn.Close(); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.