Question

I try to connect to my database and I can do that using following code:

using (SqlConnection conn =
    new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=DocumentManager; Persist Security Info=True; Integrated Security=True")) 
{ ... }

But, when I try this:

string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString)) { ... }

it doesn't work anymore. My Connection String looks like this:

<add name="DatabaseConnection" connectionString="Data Source=.\\SQLEXPRESS; Initial Catalog=DocumentManager; Persist Security Info=True; Integrated Security=True" />

And, I can read the connectionString variable and it looks exactly like the string in the first case.

Was it helpful?

Solution

Your in-code connection string has an escaped "\" in it.

Try changing your web.config to:

<add name="DatabaseConnection" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=DocumentManager; Persist Security Info=True; Integrated Security=True" />

The "\" does not need to be escaped in your web.config.

This is an example of escaping the backslash.

OTHER TIPS

Try this

string connectionString = System.Configuration.ConfigurationManager.AppSettings("DatabaseConnection");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top