[Solved] ASP.Net Core MVC Error: The ConnectionString property has not been initialized

leila398
 
on Nov 17, 2021 05:48 AM
316 Views

My connection string seems to be null, not sure how to get it to reference it correctly.

My connection string in appsettings:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DevConnection": "Data Source=xyz;Initial Catalog=xyz;User ID=xyz Password=xyz"
  }
}

Then I am using:

private readonly string connString = System.Configuration.ConfigurationManager.AppSettings["DevConnection"];
SqlConnection conn = new SqlConnection(connString);
var command = new SqlCommand(GetReadQuery(), conn);
conn.Open();

But get The ConnectionString property has not been initialized.

In debug mode connString = null but not sure why it's not getting the string.

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Nov 17, 2021 06:07 AM