namespace not working in class library c# - c#

I am trying to create a class library using some classes that I created in another project. One of the classes uses images and needs the System.Drawing namespace. However, when I try to copy the code from my project into a new class in my class library, I get an error saying the image object does not exist in the current context, and
The type or namespace name "Drawing" does not exist in the namespace System(are you missing a using directive or an assembly reference?)
It works fine in the other class as part of the other project. Why would this be?

Make sure that System.Drawing is added to the library's assembly references. By default, for class libraries, no Windows Forms assemblies are added.

Yes The problem lies in the References. If you go to your Solution Explorer and expand it, you will see a nested Folder Titled 'References'. Here is where you add the references needed in your project. To add them, simply right click the folder and select add Reference. Once here the reference you need will be in the .Net tab.

In case if a particular .NET reference is not added by default, do that manually:

using System.Drawing;
Write this in top of your class file. then also if u get error right click on reference and add reference of System.Drawing in your project.

Related

The type or namespace name 'X' could not be found

I have a CustomerOrderContext class in CustomerOrder.Data project in my solution. In another project, CustomerOrder.App, I have CustomerVM class. In this class I'm creating a CustomerOrderContext instance. But I get this following error:
Error 2 The type or namespace name 'CustomerOrderContext' could not
be found (are you missing a using directive or an assembly
reference?)
I have changed Build Actions of both projects to Page and then back to None and I have started to get this error. I have this line in CustomerVM:
using CustomerOrder.Data;
so I guess I shouldn't get this error. CustomerOrder.Data is also in References list of CustomerOrder.App. Can you tell me how I can fix this problem?
Thanks.
The issue is solved when I created new project, copied & pasted all classes.
be sure that CurstomerOrderContext class is marked as Public and not Internal.
Can you check if the framework versions are compatible? See https://stackoverflow.com/a/4286625/2478357.
Also, if you use Entity Framework (and CustomerOrder.Data uses EF), check if your CustomerOrder.App has a reference to the Entity framework dll. (This also applies to other imported dll's..)
you need to add Reference of that project (.dll) in your project.
You will find it when you right click your solution and add the (.dll) of other project in debug folder.
To access the class you must have same namespace
and
Check that both projects follow the same .Net Framework

I Can't access my class library in a web application

I have added Class Library in my web Project through Add reference option, but i am facing an error
Error 2 The type or namespace name 'UserDetailsDll' could not be
found (are you missing a using directive or an assembly reference?)*
I hope some one will help me.
Thanks in Advance.
There are a few possibilities as to why that is happening:
You're missing a using statement. Try using the whole name of the class (namespace, then class name) you're trying to use.
Your DLL is targeting a different processor architecture. Check the properties of your web application and your DLL and see if they are both targeting the same CPU.
Your DLL is for a different .NET framework than your web application. Check the target framework in the project properties.
Finally, check the references in Visual Studio for a little yellow warning icon next to your referenced DLL. It's presence indicates that the DLL couldn't be found / referenced in the first place.
You need to either add a using in your code file or specify the full name to the class you want to use.
Both solutions can be autogenerated. Rightclick on the class that won't get resolved and click on Resolve -> using XXXX

Building a re-usable class library in C#

I have a class library I've built for a coworker to use that, after giving her the .dll file, I cannot get to work. The name of the .dll file is "BatchDashboard." The name of the namespace is "BatchDashboard," and the name of the class (there is only one) is "BatchDashboard." Is it a problem to have all three similarly named? Visual Studio is able to add the reference just fine. However, this statement:
using BatchDashboard;
spits out the following error:
The type or namespace name 'BatchDashboard' could not be found (are you missing a using directive or an assembly reference?
Likewise, I cannot instantiate a new 'BatchDashboard' object.
Could someone tell me what I am doing wrong?
Thx!
EDIT
I have tried adding the reference to another test project on my computer and receive the same results.
SECOND EDIT
Changing the access modifier to public for the "BatchDashboard" class fixed the issue with the using statement. However, when I try to instantiate an object like so:
BatchDashboard batch = new BatchDashboard();
I got the following error:
'BatchDashboard' is a namespace but is used like a 'type'
It was necessary for me to have different class and namespace names to work. Thank you!
I think, I know what problem you have. When you created your class file, you probably didn't change access to your class to "public". In .Net, unless you have public members in the assembly, the namespace wouldn't be found in "using" directive.
Basically, check if the class you have is a "public class"
Here are my best guesses:
You compiled it against the wrong CPU target
You have a dependency on a Runtime library that should be added as a reference to the
project also.
Make sure that:
The library is added as reference to the project
That library is in a equal or prior .NET version compared to your project's .NET version.

c# Appsettings giving error: The name 'ConfigurationManager' does not exist in the current context

At the top of my program I have the following :
using System.Configuration;
Within my code, I have the following:
int CompanyID = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]
.ToString());
I am getting the following error though :
The name 'ConfigurationManager' does not exist in the current context
I am not sure what I am missing.
To expand a bit, you will need to add a reference to System.Configuration.dll to get this to work. It's kind of misleading because the System.Configuration namespace also exists inside the base System.dll, and holds some far lesser used objects like SettingsContext. As a result, it seems like it really ought to work, but it doesn't. It is really confusing, and currently one of those obtuse gotchas in the .NET framework.
Fortunately, System.Configuration.dll is in the .NET base framework, so you only need to add a reference by right-clicking on the References folder in your project, clicking Add Reference, and then finding System.Configuration under the .NET tab.
After it has been imported into your project, don't forget to add using System.Configuration to the top of the code file you intend to use ConfigurationManager in.
You need to add a reference to System.Configuration in your project.

System.Drawing does not exist?

I'm trying to create a validation image using class library in asp.net, but that is not the question.
Anyway, my question is... well... system.drawing does not exist so I cant use "bitmap".
From similar questions, people tell to add reference. But I already did that, restarted my computer etc. It doesn't work.
What can I do besides adding reference? It doesn't work or am I doing it wrong ?
Here is basically what I did:
Right click to references, which is under solution,
add reference
add system.drawing
But still, it does not appear in intellisense or tells me "it does not exist, try adding reference" when I type it manually.
I suspect that you added the System.Drawing reference to the wrong project. Please verify that the Validator.cs file in the project that has the System.Drawing reference.
You can add referrence 'System.Drawing.dll' to your project.
I had the same issue and solved it, click on the project menu, then Add reference, reference manager will open, then click on the assemblies tab, search for System.Drawing and check it then press ok. it will recognize the package or name space.
I stumbled this error even referencing assembly. After thinking i realised that that error produced after adding a class file which is under 'System' folder which i produced on the project folder. That class has ##.System.## namespace as well under the folder. .Net produced error if you have any custom namespace which contains 'System' keyword. Try changing folder name and namespace if u stumbled as well.
Click on the Project tab and then on Add Reference…
Then select System.Drawing or whatever you want.
In addition to adding the reference, you will also need to import it using the using statement in the top of your code file:
using System.Drawing;
You should change .NET Framework version to .NET 4.0
I have also encountered the same problem. I found that a console/class library project cannot add a reference to System.Drawing. When I changed to use a Window Form project, I was able to add a Bitmap with a reference to System.Drawing.
The error is because you have not added any user control to your class library project. Add a user control to your class library project, and the error will vanish.
I had same problem. I was trying to add Reference to System.Drawing.dll via System Explorer, but it didn't help me. Finally, I put cursor on the varialbe of type 'Graphics', clicked Ctrl+. or 'Alt+Enter->Refactoring' and select 'using System.Drawing'. I don't understand why and how, but it solved my problem.

Categories