I am trying to access connectionStrings
from the config file. The code is ASP.NET + C#. I have added System.Configuration
to reference and also mentioned with using. But still it wouldn't accept the assembly.
I am using VSTS 2008. Any idea what could be the reason?
Another weird thing is the assembly name shown as "System.configuration", a lower case c which is not how names are displayed for other System assemblies.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Utility
{
public class CommonVariables
{
public static String ConnectionString
{
get { return ConfigurationManager.ConnectionStrings["EmployeeEntities"].ConnectionString; }
}
}
}
Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="qbankEntities" connectionString="metadata=res://*/qbankModel.csdl|res://*/qbankModel.ssdl|res://*/qbankModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Ok.. it worked after restarting the VSTS. The link suggested the solution for the same problem. Wish i could have seen it before. :)
For a sanity check, try creating a new Web Application Project, open the code behind for the Default.aspx page. Add a line in Page_Load to access your connection string.
It should have System.Configuration added as reference by default. You should also see the using statement at the top of your code file already.
My code behind file now looks like this and compiles with no problems.
This assumes I have a connection string in my web.config with a name equal to "MyConnectionStringName" like so...
Yeah, it's elementary I know. But if you don't have any better ideas sometimes it helps to check against something really simple that you know should work.
I have gotten a better solution for the issue
configurationmanager does not exist in the current context
.To a read connection string from
web.config
we need to useConfigurationManager
class and its method. If you want to use you need to add namespace usingSystem.Configuration
;Though you used this namespace, when you try to use the
ConfigurationManager
class then the system shows an error “configurationmanager does not exist in the current context”. To solve this Problem:Are you sure you have added a reference to the .NET assembly and not something else? I'd remove your reference and then try re-adding it, making sure you select from the .NET tab in Visual Studio reference dialogue - the latest version should be 2.0.0.0 in GAC.
You may also get this error if you add a reference to a different, unrelated project by mistake. Check if that applies to you.
To Solve this problem go to Solution Explorer And right click reference and click add reference and chose .net and find system.configuration select an click ok