MVC web application not recognizing custom class - c#

In my test MVC web application, I've created custom class, which retrieves data from database. I have created also another folder in MVC app called Data and i have placed that data class in this folder. When i want to use this class from Controller and when importing with:
using TestMvcApp.Data;
i get an error:
The type or namespace name 'Data' does not exist in the namespace 'TestMvcApp'
(are you missing an assembly reference?)
Where can be the problem? Why it cannot find my class? I have tried a lot of things like placing that class in controller folder, but nothing helped yet. I created my test MVC app according to this article Using MvcContrib Grid in ASP.NET MVC Project Thanks for tips and answers.
EDIT: Name of my class is "ProductDB" and i have specified the namespace in it. I imported that class into project, than i created DATA folder, then i placed that class in there and finally i have changed its namespace name to TestMvcApp.Data! That class is from my another project.

I don't know why, but in my case when I added the custom class, Visual Studio 2013 didn't mark it to compile. Instead, it was marked as "Content".
I got to know it because I edited the .csproj and my class was placed just as "Include". I changed it to "Compile" and voilá, it worked!
You may use the "Build Action" class's file property in the Properties Window.

Did you create the class before or after you put it in the "Data" folder? If it was before, does it still just belong to the TestMvcApp namespace, or did you change its namespace to be TestMvcApp.Data? If not, you need to either change the namespace the class is in or change the using statement.

If your class is called Data and your namespace is called TestMvcApp.Data I suspect that there is a naming collision.

You must also set the namespace of the class to TestMvcApp.Data

Related

.Classes next to the namespase in a class

I get this strange thing when I create a new class, Next to the namespace name I get a .Classes which all I can tell prevents me from creating an instance of the class and hides it.
I would include the screen shot but im not sure how I do this. Please help so you can better understand the question.
Thanks
The namespace within Visual Studio will depend on two things:
The default namespace defined within the project properties.
The folder structure within your project.
I think that within your project you have a folder Classes where you put (well as the name guesses) your classes. Due to this fact Visual Studio will automatically append this name to the default namespace.
So either manually change the namespace manually right after you added a new class or move the file one level up within your folder structure of the project.

ASP.Net MVC Controller cannot see another namespace

I have an MVC Controller that cannot see another namespace in the using statement.
Specifically this line of code:
using TRN.Website.Tools;
Errors with:
The type or namespace name 'Tools' does not exist in the namespace
'TRN.Website' (are you missing an assembly reference?)
Other parts of my project can see the TRN.Website.Tools namespace however.
I tried adding the namespace to web.config but this had no effect.
EDIT: Sorry all I have missed out a vital bit of information. TRN.Website.Tools is just a folder with the namespace TRN.Website.Tools in the same project. It is not a separate project or a dll.
My guess: You have multiple projects in you solution and you have multiple libraries (dlls) that their names starts with TRN.Website and you added reference to one of these dlls in your MVC project. you have to add reference to the other one too.
Solved but in a really strange way.
I added another class in the Tools folder and after that the error disappeared.
Very very odd.
Check your Build Action of your class in properties window. It must be 'Compile'.
Your ASP.NET MVC project does not contain a reference to the assembly that contains the TRN.Website.Tools namespace. You have to reference that from your ASP.NET MVC project in order to use it.
If TRN.Website.Tools is a project in your Solution, you can just add a Project reference to it.
If it isn't a project in your Solution, you'll have to add a reference to the compiled DLL to your ASP.NET MVC project.
In my case, I had not checked in the "X.csproj" file to TFS, so the new controller I created was not being found in my testing environments as a valid controller.

The type or namespace name does not exist - folders in C#

I'm sure there is a simple solution to this problem, but it has defeated me so far. Basically all I'm trying to do is include some classes in a separate folder in my c# project.
Strangely this has been working just fine until today.
In the solution explorer I created a new folder called animations.
I added the line to the main class:
using AnimationEditor.animations; (AnimationEditor is the solution name/namespace)
which throws the error:
Error 1 The type or namespace name 'animations' does not exist in the namespace 'AnimationEditor' (are you missing an assembly reference?)
As I said, I didn't have this error before today so I am a little confused.
If you don't have the the line namespace AnimationEditor.animations in the the class you are trying to reference you need to manually add it.
Those namespace statements do not get automatically added when you move a file, they only get automatically put in when you create a new file under the folder.
So your class should look like
namespace AnimationEditor.animations
{
class MyClass
{
//snip
}
}
As a side note, the .NET naming conventions state you should use a capital letter for those sub namespaces, capitalize the folder name and it will automatically capitalize the namespace for new files (you will need to manually change existing ones, just like moving)
If you drag files to another folder (or add them) in visual studio, namespace does not change automatically (atleast in 2010). Check namespace of AnimationEditor class.

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.

Type or namespace could not be found from App_code folder

I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:
The type or namespace name 'ArchivedFilesWrapper' could not be found (are you missing a using directive or an assembly reference?)
I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?
Here is a snippet from my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMCWebAdmin.App_Code
{
public class ArchivedFilesWrapper
{
Perhaps the problem will be solved by changing the Build Action Property of the *.cs source file to Compile from Content. From the Solution Explorer right click on the source file and choose Property.
Note that the App_Code folder is intended for use in Web Site Projects.
Note that for a Web Application Project or MVC project, adding an App_Code folder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Code folder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Property of the *.cs source file to Compile from Content the namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.
Important
In the MVC or Web Application project, the App_Code folder is trouble because it has Web Site Project type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twice errors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Code folder in the different project types see this blog
You need to add
using EMCWebAdmin.App_Code;
to all the pages you want to be able to use the class.
Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin
then in your class change
namespace EMCWebAdmin.App_Code
{
...
to
namespace EMCWebAdmin
{
...
This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.
If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.
Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue.
Thanks johnmcp.

Categories