0

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.

3
  • What you mean by it doesn't work anymore.? Commented Apr 7, 2014 at 18:57
  • What error message do you get? Commented Apr 7, 2014 at 18:57
  • There is no error. I try to display data from database and there is no data. Commented Apr 7, 2014 at 18:59

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

string connectionString = System.Configuration.ConfigurationManager.AppSettings("DatabaseConnection");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.