How to access web.config connection string in C#?
        Posted  
        
            by salvationishere
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by salvationishere
        
        
        
        Published on 2010-04-24T17:11:32Z
        Indexed on 
            2010/04/24
            17:13 UTC
        
        
        Read the original article
        Hit count: 258
        
I have a 32-bit XP running VS 2008 and I am trying to decrypt my connection string from my web.config file in my C# ASPX file.
Even though there are no errors returned, my current connection string doesn't display contents of my selected AdventureWorks stored procedure.
I entered it:
C:\Program Files\Microsoft Visual Studio 9.0\VC>Aspnet_regiis.exe -pe "connectionStrings" -app "/AddFileToSQL2"
Then it said "Succeeded".
And my web.config section looks like:
  <connectionStrings>
    <add name="Master" connectionString="server=MSSQLSERVER;database=Master; Integrated Security=SSPI"
      providerName="System.Data.SqlClient" />
    <add name="AdventureWorksConnectionString" connectionString="Data Source=SIDEKICK;Initial Catalog=AdventureWorks;Integrated Security=True"
      providerName="System.Data.SqlClient" />
      <add name="AdventureWorksConnectionString2" connectionString="Data Source=SIDEKICK;Initial Catalog=AdventureWorks;Persist Security Info=true; "
  providerName="System.Data.SqlClient" />
  </connectionStrings>
And my C# code behind looks like:
    string connString = ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString2"].ConnectionString;
Is there something wrong with the connection string in the web.config or C# code behind file?
© Stack Overflow or respective owner