title: Application Role connect to SQL Server with "Login Failed" error
body: <p>I have a program which would use the Application Role to write data to a SQL Server 2005.</p>
  
  <pre><code>using (SqlConnection sqlCon = new SqlConnection(connectionString))
  {
     SqlCommand sqlCommand = new SqlCommand();
     sqlCommand.Connection = sqlCon;
     sqlCommand.CommandType = CommandType.Text;
     sqlCommand.CommandText = "";
     sqlCommand.CommandText = "EXEC sp_setapprole 'name','password';";
     sqlCommand.CommandText += sqlComm;
     sqlCommand.CommandTimeout = 300;
     sqlCon.Open();
  
     int res = sqlCommand.ExecuteNonQuery();
  }
  </code></pre>
  
  <p>I use this code to connect a SQL Server 2005 ServerA, it works well. Then I use the same code to connect anohter SQL Server 2005 ServerB, with the identical table, application role, it gave an error."Login Failed for domain\username" Does anyone meet it before?</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException was
    unhandled by user code<br>
    Message="Login failed for user
    'domian\username'."   Source=".Net
    SqlClient Data Provider"<br>
    ErrorCode=-2146232060   Class=14<br>
    LineNumber=65536   Number=18456<br>
    Procedure=""   Server="ServerB"<br>
    State=1<br>
    StackTrace:<br>
      at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
    exception, Boolean breakConnection)<br>
      at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
    stateObj)<br>
      at System.Data.SqlClient.TdsParser.Run(RunBehavior
    runBehavior, SqlCommand cmdHandler,
    SqlDataReader dataStream,
    BulkCopySimpleResultSet
    bulkCopyHandler, TdsParserStateObject
    stateObj)<br>
      at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean
    enlistOK)<br>
      at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo
    serverInfo, String newPassword,
    Boolean ignoreSniOpenTimeout, Int64
    timerExpire, SqlConnection
    owningObject)<br>
      at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String
    host, String newPassword, Boolean
    redirectedUserInstance, SqlConnection
    owningObject, SqlConnectionString
    connectionOptions, Int64 timerStart)<br>
      at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection
    owningObject, SqlConnectionString
    connectionOptions, String newPassword,
    Boolean redirectedUserInstance)<br>
      at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
    identity, SqlConnectionString
    connectionOptions, Object
    providerInfo, String newPassword,
    SqlConnection owningObject, Boolean
    redirectedUserInstance)<br>
       at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions
    options, Object poolGroupProviderInfo,
    DbConnectionPool pool, DbConnection
    owningConnection)<br>
       at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection
    owningConnection,
    DbConnectionPoolGroup poolGroup)<br>
       at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
    owningConnection)<br>
       at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
    outerConnection, DbConnectionFactory
    connectionFactory)<br>
       at System.Data.SqlClient.SqlConnection.Open()<br>
       at ADSK.PSEB.ACRSubmitComponent.ACRSubmit.backgroundUploadWork_DoWork(Object
    sender, DoWorkEventArgs e) in
    C:\Documents and
    Settings\lvlu\Desktop\Mulder\Upload\ADSK.PSEB.ACRSubmitComponent\trunk\ADSK.PSEB.ACRSubmitComponent\Form1.cs:line
    897<br>
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs
    e)<br>
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object
    argument)   </p>
  </blockquote>