How to include special characters in connection string

How to include special characters in web.config file?

You will get an error if your password contains an ampersand character (e.g.: p&ssword) are not encoded in the web.config file.

I think you've probably used an automatically generated password or a password that contains left angle brackets. In this situation you will encounter an unsuccessful connection to the database.

Remember that web.config is an XML file so you must be encoded special characters.

Instead of using

<add name="db" connectionString="Data Source=.;Initial Catalog=mydb;User ID=sa;Password=p&ssw0rd;" providerName="System.Data.SqlClient" />

try

 <add name="db" connectionString="Data Source=.;Initial Catalog=mydb;User ID=sa;Password=p&amp;ssw0rd;" providerName="System.Data.SqlClient" />

You will need to do this for all the 'special' characters below (to solve the connection string password with special characters)

< = &lt;
> = &gt;
" = &quot;
' = &apos;
& = &amp;

This post answers the following common related questions:

"&" character breaks passwords that are stored in the web.config file

'&' in password illegal character in web.config file

How to include ampersand in connection string using web.config file

How to escape quote in web.config connection string

How to escape an '&' in the password of a connection string in web.config file

Related