28

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

Scubacode
  • 415
  • 1
  • 4
  • 16
  • 2
    Which line generates the error? – ic3b3rg Jun 27 '13 at 13:19
  • Can you tell us where are yu getting the error – Microsoft DN Jun 27 '13 at 13:40
  • 2
    Guidance: 1) assembly loaded?, 2) assembly loaded matches with origin assembly?, 3) "using" directives pointing to old or none valid references?, 4) .csproj manifest includes source invalid?, 5) a search tool looking regex in the entire solution (every class library and project). 5) check project settings for net framework version build option (collaborate in teams bring on this kind of problen, you must agree net framew. build version in both sides) 6) After that clean and build each by separate and finally, include all references to the destination project/class library. I should work! – Felix Aballi May 26 '16 at 14:48

12 Answers12

33

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

John Kraft
  • 6,811
  • 4
  • 37
  • 53
  • Yes, this is probably the reason why. – Abijeet Patro Jun 27 '13 at 13:23
  • You also can read this http://msdn.microsoft.com/en-us/library/vstudio/wkze6zky.aspx – Lucas Jun 27 '13 at 13:58
  • 1
    Guidance: 1) assembly loaded?, 2) assembly loaded matches with origin assembly?, 3) "using" directives pointing to old or none valid references?, 4) .csproj manifest includes source invalid?, 5) a search tool looking regex in the entire solution (every class library and project). 5) check project settings for net framework version build option (collaborate in teams bring on this kind of problen, you must agree net framew. build version in both sides) 6) After that clean and build each by separate and finally, include all references to the destination project/class library. I should work! – Felix Aballi May 26 '16 at 14:49
14

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

Gayatri Dingare
  • 162
  • 1
  • 3
3

If you've tried the above solutions and haven't found the answer, make sure that the .NET versions of all projects are the same.

I ran into this problem when importing a .NET version 4.6.1 into a .NET version 4.6.2 project. Without any warnings from Visual Basic!

More Info: The type or namespace name could not be found

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Caleb Laws
  • 71
  • 5
1

Your using statements appear to be correct.

Are you, perhaps, missing the assembly reference to System.configuration.dll?

Right click the "References" folder in your project and click on "Add Reference..."

Mac
  • 373
  • 5
  • 20
Samuel Parkinson
  • 2,992
  • 1
  • 27
  • 38
0

This problem would be caused by your application missing a reference to an external dll that you are trying to use code from. Usually Visual Studio should give you an idea about which objects that it doesn't know what to do with so that should be a step in the right direction.

You need to look in the solution explorer and right click on project references and then go to add -> and look up the one you need. It's most likely the System.Configuration assembly as most people have pointed out here while should be under the Framework option in the references window. That should resolve your issue.

Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
0

I have observed a quote ' in your 1st line and also at the end of your last line.

'using System.Collections.Generic;

Is this present in your original code or some formatting mistake?

Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
  • 1
    no it's not, new to this site and got a pop up saying to add them in so it would format correctly in my question – Scubacode Jun 27 '13 at 18:04
0

I had the same problem earlier today. I could not figure out why the class file I was trying to reference was not being seen by the compiler. I had recently changed the namespace of the class file in question to a different but already existing namespace. (I also had using references to the class's new and previous namespaces where I was trying to instantiate it)

Where the compiler was telling me I was missing a reference when trying to instantiate the class, I right clicked and hit "generate class stub". Once Visual Studio generated a class stub for me, I coped and pasted the code from the old class file into this stub, saved the stub and when I tried to compile again it worked! No issues.

Might be a solution specific to my build, but its worth a try.

Alex Vallejo
  • 1,331
  • 1
  • 12
  • 15
0

In some cases, when necessary using has been obviously added and studio can't see this namespace, studio restart can save the day.

Tropin Alexey
  • 606
  • 1
  • 7
  • 16
0

I was getting warnings about different versions in .NET framework; I ignored them.

The project compiles fine making the change in the solution's properties.

hestellezg
  • 3,309
  • 3
  • 33
  • 37
0

I'm using Visual Studio Code and could not use instructions from above so I found another way to fix the problem with referencing to namespace from another file. All what need to be done is to add include to your .csproj file e.g:

<ItemGroup>
<Compile Include="filename.cs" />
</ItemGroup>

Then you can use namespaces from filename.cs

Karol
  • 1
  • 1
0

Right click on project name and Unload Project.

Then right click again on the project name and reload the project.

RAMP
  • 1
  • 1
-2

The following technique worked for me:

1) Right click on the project Solution -> Click on Clean solution

2) Right click on the project Solution -> Click on Rebuild solution

Akash V
  • 11