1

I would like to connect to an existing database, and I put the connection string in the ConnectionStrings section of the Web.Config. But where and how do you refer to this connection in the code. For example, I try things like

string connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabase"].ToString();

in my C# code right before I read the database.

The only way I can make it work is to hard-code the connection string in the C# code, because I don't know how to get it from Web.Config.

1
  • Why didn't the snippet you posted work? Commented May 24, 2011 at 21:23

1 Answer 1

1

That code you have is correct (i fixed one small typo with it). Make sure you add a reference to System.Configuration to your project. And obviously make sure that "MyDatabase" exists as a connection string in the project's web.config.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.