Reputation: 113
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.
Upvotes: 0
Views: 127
Reputation: 18162
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.
Upvotes: 2
Reputation: 4185
Try this
string connectionString = System.Configuration.ConfigurationManager.AppSettings("DatabaseConnection");
Upvotes: 0