I'm trying to allow a user to enter data into a textbox that will be added to the web.config file. I've added the relevent lines to the web.config file but when I make this class all goes wrong.
I keep getting the are you missing a using directive or assembly refenrence error whenever I try to run my app. I have looked at the other times this question has been asked and can't seem to figure out where I'm going wrong. The thing is that I am extremely new to Visual Studio and am just left blank at what could be the answer.
Below here is the class file that's generating the error. I hope I've included everything you need to assist me. Thank you.
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
In the Project Designer, click the References tab. Click the Add button to open the Add Reference dialog box. In the Add Reference dialog box, select the tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.
Reference assemblies are usually distributed with the Software Development Kit (SDK) of a particular platform or library. Using a reference assembly enables developers to build programs that target a specific library version without having the full implementation assembly for that version.
You probably don't have the System.Configuration dll added to the project references. It is not there by default, and you have to add it manually.
Right-click on the References and search for System.Configuration in the .net assemblies.
Check to see if it is in your references...
Right-click and select Add Reference...
Find System.Configuration in the list of .Net Assemblies, select it, and click Ok...
The assembly should now appear in your references...
.Net framework of the referencing dll should be same as the .Net framework version of the Project in which dll is referred
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With