I have the following connection string declared in my app.config
file:
<connectionStrings>
<add name="SqlConnectionString" connectionString="Data Source=xxx.xx.xx.xx;Initial Catalog=xxxxx;User Id=xx;Password=xxx;" providerName="System.Data.SqlClient" />
</connectionStrings>
When I try to fetch this connection string using the following C# code snippet, I get the value null
. I am not able to obtain the connection string. Is there anything wrong in the syntax?
First attempt:
var settings = ConfigurationManager.ConnectionStrings["SqlConnectionString"];
string result = settings.ConnectionString;
Second attempt:
string result = ConfigurationSettings.AppSettings["SqlConnectionString"];