0

I am trying to get the connection string from Azure Application settings using environment variables, But it seems that the format of the connection string that I am putting in Azure App setting is not proper.Here is my original connection string that works fine in localhost.

    <add connectionString="Server=tcp:****.database.windows.net,1433;Initial Catalog=***;
Persist Security Info=False;User ID=******;Password=*********;MultipleActiveResultSets=False;
    Encrypt=True;TrustServerCertificate=False;Connection Timeout=1200000;
    Max Pool Size=500;Pooling=true;" name="Con_String"></add>

I am putting "Server=tcp:****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=1200000;Max Pool Size=500;Pooling=true;"as the con_string value in Azure Application Setting

Now for fetching the Connection string at runtime I am using Environment variable as string ConnectionString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Con_String");

But during running the web app I am getting the exception message Keyword not supported: '"Server'.

I tried the approaches in Retrieve and use Windows Azure's connection strings? and other similar posts ,but didn't work. Am I missing something silly here?

rkm
  • 13
  • 1
  • 6
  • When you entered the value in application settings, did you include the double quotes (`"`) as well? If that's the case, can you try by removing them? So the value you will enter would be `Server=tcp:****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=1200000;Max Pool Size=500;Pooling=true;` (without starting and ending double quotes). – Gaurav Mantri Jan 18 '18 at 06:10
  • @GauravMantri I tried it and It's giving a different exception message **The ConnectionString property has not been initialized.** – rkm Jan 18 '18 at 06:27
  • 1
    Please have a try to use the following code. `ConnectionString =ConfigurationManager.ConnectionStrings["Con_String"].ConnectionString`. According to your supplied screenshot. `Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Con_String")` should work. – Tom Sun - MSFT Jan 18 '18 at 06:35
  • Thanks @GauravMantri. Removing the double quotes worked for me,Actually I had forget to save the Appsetting after the change.Sorry for the immature previous reply! – rkm Jan 18 '18 at 07:10

2 Answers2

1

Am I missing something silly here?

If you still get the Keyword not supported: '"Server' or The ConnectionString property has not been initialized.

I assume that you don't save the appsetting after you add the connection string the WebApp appseting.

enter image description here

The following code are both working on my side.

ConnectionString = ConfigurationManager.ConnectionStrings["Con_String"].Connec‌​tionString.

ConnectionString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Con_Stri‌​ng")

Test Result:

enter image description here

Note: as GauravMantri mentioned that there is no need quotes (") in the connection string in the Azure WebApp appsetting.

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Seriously It was a very SILLY mistake from me! Actually GauravMantri's suggestion was perfect. and What you mentioned that I had forget to save the webApp setting. Thanks a ton to both of you as it has solved my headache of 3 days. – rkm Jan 18 '18 at 07:08
0

In my case issue was connected with the stupid mistake: when copy/pasted connection string to Notepad++ I somehow missed that newline operator appeared because of the Notepad++ was wrapping the text or smth else.

In any case, I've copy/pasted to Notepad then replaced User ID and Password, then copy/pasted again to the server, Saved the settings then Restarted the server. Finally, issue fixed.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42