Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting the "missing a using directive or assembly reference" and no clue what's going wrong

Tags:

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

like image 413
Scubacode Avatar asked Jun 27 '13 13:06

Scubacode


People also ask

How do you use assembly references?

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.

What is reference in assembly?

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.


2 Answers

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

enter image description here

Right-click and select Add Reference...

enter image description here

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

enter image description here

The assembly should now appear in your references...

enter image description here

like image 178
John Kraft Avatar answered Sep 29 '22 20:09

John Kraft


.Net framework of the referencing dll should be same as the .Net framework version of the Project in which dll is referred

like image 23
Gayatri Dingare Avatar answered Sep 29 '22 18:09

Gayatri Dingare