title: Connection problem when changing password
body: <p>I have a software which displays some tables from our SQL database. Now I want to add a tool, where I can change the password for my "testdummy" user.</p>
  
  <p>I tried to open a connection again but it didn't help. If you need some additional code or informations, just write a comment.</p>
  
  <p>Notice that I'm new to programming. I'm a apprentice and currently learning programming and administer databases. I know this is not the safest solution, but it's just a little task from my instructor. This software will not be released for customers.</p>
  
  <p>Like I mentioned before, I tried to open a connection again before I want to change the password.</p>
  
  <pre><code> public void Change()
          {
              SqlConnection con = new SqlConnection();
              string connectionString = GetConnectionString();
  
  
              if (NewPassword.Password == NewPasswordAgain.Password &amp;&amp; OldPassword.Password == GlobalData.Password)
              {
                  try
                  {
                      //dbMan.TryConnect(connectionString);
                      //con.Open();
                      SqlConnection.ChangePassword($"Data Source={GlobalData.ServerName};Initial Catalog={GlobalData.DBName};UID={GlobalData.Username};PWD={OldPassword}", $"{NewPassword}");
                  }
                  catch (SqlException ex)
                  {
                      MessageBox.Show("Unable to change password. Try again!" + ex);
                  }
              }
              else
              {
                  // If new Password doesn't match.
                  MessageBox.Show("Passwords doesn't match!");
              }
          }
  </code></pre>
  
  <p>I'm getting a SQL exception when I am trying to change the password.</p>
  
  <pre><code>(System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'csharptest'.
  </code></pre>
  
  <p>I get this at:</p>
  
  <pre><code>SqlConnection.ChangePassword($"Data Source={GlobalData.ServerName};Initial Catalog={GlobalData.DBName};UID={GlobalData.Username};PWD={OldPassword}", $"{NewPassword}");
  </code></pre>
  
  <p>At this point of the programm, there should be a connection to the database, because I can handle some tables and manipulate the data sets.</p>
  
  <p>But when I uncomment this:</p>
  
  <pre><code>//dbMan.TryConnect(connectionString);
  //con.Open();
  </code></pre>
  
  <p>It goes into the catch brackets there:</p>
  
  <pre><code>public bool TryConnect(string connectionString)
          {
              conn = new SqlConnection();
              conn.ConnectionString = connectionString;
  
              try
              {
                  conn.Open();
                  return true;
              }
              catch (Exception)
              {
                  MessageBox.Show("Couldn't connect");
                  return false;
              }
          }
  </code></pre>
  
  <p>and returns following exception:</p>
  
  <pre><code>System.InvalidOperationException: 'Die ConnectionString-Eigenschaft wurde nicht initialisiert.'
  </code></pre>
  
  <p>In english it should be something like: "the connectionstring property has not been initialized"</p>
  
  <p>Edit:
  In the logs I'm getting this:</p>
  
  <pre><code>Login failed for user 'csharptest'. Reason: Password did not match that for the login provided.
  </code></pre>
  
  <p>Edit:
  Instead of:</p>
  
  <pre><code>SqlConnection.ChangePassword($"Data Source={GlobalData.ServerName};Initial Catalog={GlobalData.DBName};UID={GlobalData.Username};PWD={OldPassword}", $"{NewPassword}");
  </code></pre>
  
  <p>I did this:</p>
  
  <pre><code>string updatePassword = "USE CSHARPTEST ALTER LOGIN [" + GlobalData.Username + "] WITH PASSWORD =  '" + NewPassword + "'";
  con.Open();
  cmd.ExecuteNonQuery();
  </code></pre>
  
  <p>And now I think the only problem is the permission on the server.</p>