views:

3769

answers:

4

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 assembleis.

Thanks!

ADDED CODE:

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 file:

<?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=&quot;Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
+1  A: 

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.

Dan Diplo
Assembly details from property window:Name: System.configurationPath: C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.configuration.dllVersion: 2.0.0.0Runtime Version: v2.0.50727Anything looks suspicious? Thanks!
pencilslate
No, that is exactly as expected. How really strange - I'm sorry, but I've never seen this before. Good luck!
Dan Diplo
A: 

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.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication1
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string connString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString;
    }
  }
}

This assumes I have a connection string in my web.config with a name equal to "MyConnectionStringName" like so...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="MyConnectionStringName"
            connectionString="Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

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.

Nevada
+1  A: 

Ok.. it worked after restarting the VSTS. The link suggested the solution for the same problem. Wish i could have seen it before. :)

pencilslate
+4  A: 

It's not only necessary to use the namespace System.Configuration. You have also to add the reference to the assembly System.Configuration.dll , by right-click-ing on the References tab, choose add reference and then find System.Configuration. This will work for sure. Also for the NameValueCollection you have to write: using System.Collections.Specialized;

Kieran
+1 Thanks Kieran. Do you know why this has to be done when most other assemblies can simply be called by including the 'using' statement?
David HAust
This is my understanding: it may be wrong. When you add the reference you are asking the dll to be copied to the bin folder on compile/ build. Some dll's seen as core are added when the project is created, ie when you go file->new project so the references are set up at that point. They all have to go through the same process just that some are done for you and some you have to do manually. You could test it out by deleting the reference to your System dll and watching all your code fail. =)
Kieran
OK. So I have done a little more research and found that above is mostly true. However some of the files will not need to be written to the bin folder on run as they are in the Global Assembly cashe (GAC), where bin is local assembly cashe. Like the System.dll.
Kieran