title: Why won't my application connect to SQL Server database when I can connect using SQL Server Management Studio with the same login?
body: <p>I realize this may be a difficult (impossible?) question to answer since I can't provide access to the actual server involved, but maybe there's something obvious I'm missing.</p>
  
  <p>I have a C# Windows Forms application (.Net 4.5) that needs to connect to a SQL Server 2012 database. The connection string in <code>App.config</code> is as follows:</p>
  
  <pre><code>&lt;connectionStrings&gt;
    &lt;add name="MyConnStr"
      connectionString="server=SERVERNAME.ads.lcl;database=MyDatabase;uid=MyUser;password=abcdefg1234567"
      providerName="AspNetSqlProvider" /&gt;
  </code></pre>
  
  <p>I am able to connect to and read from the database with SQL Server Management Studio using the same login and password (with SQL Server Authentication), so I know I have the server, database, uid, and password correctly specified in the connection string. But when I debug my program, I get an SQLException saying <code>Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'MyUser'.</code></p>
  
  <p>I've done this a thousand times before, so I'm at a loss.</p>
  
  <p>Here are the relevant sections of code, if it helps at all:</p>
  
  <pre><code>public static string MyConnStr
  {
    get { return ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString; }
  }
  
  public static int[] GetIntArrayFromDB(string query)
  {
    DataTable dt = new DataTable();
  
    using (SqlConnection conn = new SqlConnection(MyConnStr))
    {
      using (SqlCommand cmd = new SqlCommand(query, conn))
      {
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
          try
          {
            conn.Open();
            da.Fill(dt);
            conn.Close();
          }
          catch (SqlException)
          {
            return null;
          }
          finally
          {
            if (conn != null)
              conn.Dispose();
          }
        }
      }
    }
  
    int numRows = dt.Rows.Count;
    int[] retInt = new int[numRows];
    for (int i = 0; i &lt; numRows; i++)
    {
      retInt[i] = Convert.ToInt32(dt.Rows[i][0]);
    }
  
    return retInt;
  }
  </code></pre>
  
  <p>The query being passed to <code>GetIntArrayFromDB()</code> is <code>"SELECT myID FROM myTable"</code>. Any ideas?</p>
  
  <p><strong>UPDATE</strong>: The approach above with the named connection string is perfectly valid and works, if you don't misspell the connection string name in the config file. Sorry for this, I'm working on a sleep deficit. Thanks to everyone who offered suggestions!</p>