I get “no link to directive or assembly” and don’t know what is going on

I am trying to allow the user to enter data in a text box that will be added to the web.config file. I added the appropriate lines to the web.config file, but when I do this class, everything goes wrong.

I keep getting it if you fail to use the build directive or refendrence error when I try to run my application. I looked at other questions that asked this question, and it can't seem like I'm wrong. The fact is that I'm unusually new to Visual Studio, and I just left a blank spot in what might be the answer.

Below is a class file that generates an error. Hope I have included everything you need to help me. Thanks.

using System.Collections.Generic; using System.Linq; using System.Configuration; namespace WebConfigDemo { public class CompanyConfigSection : ConfigurationSection { [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)] public CompanyConfigCollection Companies { get { return (CompanyConfigCollection)this[""]; } set { this[""] = value; } } } public class CompanyConfigElement : ConfigurationElement { [ConfigurationProperty("id", IsKey = true, IsRequired = true)] public int Id { get { return (int)this["id"]; } set { this["id"] = value; } } [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return this["name"].ToString(); } set { this["name"] = value; } } } ' public class CompanyConfigCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new CompanyConfigElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((CompanyConfigElement)element).Id; } } public class CompaniesConfig { private static readonly Dictionary<int, CompanyConfigElement> Elements; static CompaniesConfig() { Elements = new Dictionary<int, CompanyConfigElement>(); var section = (CompanyConfigSection)ConfigurationManager.GetSection ("companies"); foreach (CompanyConfigElement system in section.Companies) Elements.Add(system.Id, system); } public static CompanyConfigElement GetCompany(int companyId) { return Elements[companyId]; } public static List<CompanyConfigElement> Companies { get { return Elements.Values.ToList(); } } } } ' 

Any help is appreciated

+10
source share
6 answers

Probably, the System.Configuration libraries have not added project references. This is not the default and you must add it manually.

Right-click the link and search for System.Configuration in .net assemblies.

Check if it is listed in your links ...

enter image description here

Right-click and select Add Link ...

enter image description here

Locate System.Configuration in the list of .Net Assemblies, select it and click Ok ...

enter image description here

Now the assembly should appear in your links ...

enter image description here

+19
source

. The network structure of the referenced DLL should be the same as the version of the .NET Framework project in which the dll refers

+4
source

Your statements about using seem correct.

Perhaps you are missing a reference to the assembly of System.configuration.dll ?

Right-click the Links folder in your project and click Add Link ...

0
source

This problem will be caused by the fact that your application does not have a link to the external dll with which you are trying to use the code. Usually, Visual Studio should give you an idea of ​​what objects it does not know what to do, so that it is a step in the right direction.

You need to look in the solution explorer and right-click on the links to the project, and then go to add → and find the one you need. Most likely, the System.Configuration assembly, as indicated by most people, should be under the Framework option in the links window. This should solve your problem.

0
source

I noticed a quote ' in your first line, as well as at the end of your last line.

 'using System.Collections.Generic; 

Is this present in your source code or some formatting error?

0
source

I had the same problem today. I could not understand why the class file I was trying to reference did not see the compiler. Recently, I changed the namespace of the class file in question to a different but existing namespace. (I also used links to new and previous class namespaces, where I tried to create it)

Where the compiler told me that I didn’t have enough links when trying to create an instance of the class, I right-clicked and clicked “generate cool stub”. After Visual Studio generated a stub class for me, I managed and pasted the code from the old class file into this stub, saved the stub, and when I tried to compile it, it will work! No problems.

It may be a solution specific to my build, but worth a try.

0
source

All Articles