2

In web.config file i have :

  <connectionStrings> 
    <add name="connectionString" connectionString="Data Source=Server; Initial Catalog=DB; Persist Security Info=true; User ID=****; Password=****" />
  </connectionStrings>

I try to reed this connection string in the class :

var connString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

But I get this error :

System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null

3
  • What's your .NET version?? Commented Dec 8, 2019 at 16:01
  • have you ensured that the config file is placed correctly at the directory which you're running the application? in the executing directory does there exist an ApplicationName.exe.config file? Commented Dec 8, 2019 at 16:02
  • I use .Net Core 3.0 Commented Dec 8, 2019 at 16:05

1 Answer 1

3

I suggest in .net core apps use appsettings.json and read connectionString from it :

   static class class
{
    public static IConfigurationRoot Configuration;

    public static string GetConnectionString()
    {
         var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");

         Configuration = builder.Build();
         var connectionString = Configuration["ConnectionStrings:connectionString"];

    }
}

appsettings.json :

{
  "ConnectionStrings": {
    "connectionString": "............."
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.