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
Scubacode
source share