3

I am working in Asp.net Razor MVC3. I have created a App_Code folder and make a class in which I want read connection string from web.config and working with database by using this class. I am writing System.Configuration in this app_code class but it didn't shows me ConfigurationManager of Configuration class. It shows like this enter image description here Rather than if I am write the same line in any controller then it will show System.Configuration.ConfigurationManager and in controller I can read connection string from web.config but in app_code I can't.

Please give me the solution how to read connection string in app_code class?

2 Answers 2

10

Include a reference to System.Web.Configuration:

using System.Web.Configuration;

Then, in your code, access it like this:

WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

Example Class:

using System.Web.Configuration;

namespace MyWebApp.App_Code
{
    public class TestClass
    {
        public void TestMethod()
        {
            var connStr = WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am working in MVC3 with framework4.0 in Visual Studio2012. While I am adding reference System.Web.Configuration into my MVC application, it doesn't show in Add Reference Manager Popup box. So i Couldn't include its reference into the project.
You cannot add System.Web.Configuration from the Add Reference Popup, you only need to add System.Web. Once that is added, then you have access to the System.Web.Configuration namespace. Also, in your original post, you stated you were trying to add "System.Configuration.ConfigurationManager". That is not what used in my answer. I stated System.Web.Configuration. Take a close look at my answer and make sure you're using the correct namespace and using WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString to get your connection string.
I got it a very strange error i.e. while I am creating an empty MVC application then this error occurs and if I am creating any Internet or Intranet MVC application then this error not occurring.
0

If you receive an error regarding "the ConfigurationManager does not exist in the current context", you will need to install the dll assembly besides for having the using System.Configuration statement in your controller.

You can find that in the Nuget gallery here https://www.nuget.org/packages/System.Configuration.ConfigurationManager/ or by added an assembly reference inside Visual Studio.

After that, you can write string MyString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString; (notice the singular form at the end of that piece of code).

Or if you haven't written the using statement for System.Configuration you will write the same thing but preface it with string MyString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

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.