Search code examples
c#connection-stringsqlconnectionhtml-escape-characters

Parse string from app.config with characters that are escaped


This is a really stupid question, but I'm getting really frustrated over it right now.

I have an app.config that store user credentials right now, for SQL Server.

So the username is reader and password is "abcdefg".

When i try to execute an insert now I get this exception:

Format of the initialization string does not conform to specification starting at index 138.

When I change the credentials to an user that doesn't have any special characters that has o be escaped it works like a charm.

Do anyone know what it can be?

The connection string that gets mashed up during a debug session is:

Data Source=MACHINE\\SQLSERVER;Initial Catalog=DbName;Connect Timeout=300;Application Name=application;User ID=reader;Password=\"abcdefg\"

Solution

  • You can escape the double quotes in the password field with "

    So the connectionStrings would look like this:

      <connectionStrings>
        <add connectionString="Data Source=MACHINE\\SQLSERVER;Initial Catalog=DbName;Connect Timeout=300;Application Name=application;User ID=reader;Password=&quot;abcdefg&quot;" name="CS"/>
      </connectionStrings>