1
votes

I have a set of C# files for a web application that I've inherited from a developer who's no longer around. I've created a solution in Visual Studio 2010 and added the existing files and subfolders to the solution from source control. One of the subfolders I created is called App_Code and mimics a subfolder that existed in our source control program. Inside this subfolder are a few C# classes. These classes are referenced in code behind files at the root (e.g., ActionPage.aspx.cs: ), but none of the code behind files can find the classes in App_Code. I get the usual error message, "The type or namespace 'ErrorLogger' [one of the classes] could not be found (are you missing a directive or an assembly reference?)" The App_Code subfolder doesn't look like a regular folder. I'm attaching a clip showing what it looks like:

What App_Code subfolder looks like in Visual Studio.

I suspect it's a special Visual Studio folder name. Here's an example of the code in ActionPage that's calling ErrorLogger (and is giving the error message listed above):

ErrorLogger Err = new ErrorLogger();

And here's what the ErrorLogger class looks like:

using System;
using System.IO;
using System.Text;

public class ErrorLogger
{

public void Logger(string sPathName, string sErrMsg)
{
    //- <--custom date time stamp & format for easy reading
    string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
    string sYear = DateTime.Now.Year.ToString();
    string sMonth = DateTime.Now.Month.ToString();
    string sDay = DateTime.Now.Day.ToString();
    //string sErrorTime = sYear+sMonth+sDay;
    string sErrorTime = sMonth + sDay + sYear;
    //- <--Write error to a file
    StreamWriter sw = new StreamWriter(sPathName + sErrorTime, true);
    sw.WriteLine("------------------------------------------------------------------------------------------------");
    sw.WriteLine(sLogFormat + sErrMsg);
    sw.Flush();
    sw.Close();
}

}

What am I missing?

Added 2/24/2015: I've found a somewhat unsatisfactory solution: I created a new VS 2010 solution and then chose File->Add->Existing Web Site. I now have the code in a website instead of a web application, but it does compile and run. Short of building the whole thing over again, I think this is the best I can do - unless anyone has any other suggestions.... And I do appreciate all the suggestions, folks, believe me. On a related topic, WHY does File->New->Project from Existing Code not give me the option for a Web Application? Redmond, if you're listening, add that!!

5
What namespaces are the classes in? The reason you can't see them is most likely namespace issues.Ryan Ternier
look up how to use namespace that will make your life a lot easier or look up how to use the using clause / statement in your class hearders if you are not familiar with how to find the namespace or default namespace then right click on AAT in the project and select properties to seeMethodMan
it doesn't look like your class has a namespace, so it will only be accessible to other classes within your project which do not have a namespace declaration.Claies
Have you tried doing a clean/build on the solution?McAden
You don't"need" namespaces for your app to work, it just things a lot nicer to work with. You mentioned you made the App_Code directory. Working with older Versions of Visual Studio this was problematic sometimes. Have you tried moving the class to the base level to see if it works there?Ryan Ternier

5 Answers

1
votes

Remove the file from the project.

Right click the project and select "Add new item..."

Select Class and name the new class ErrorLogger.

Paste in the existing code.

1
votes

I just found this link; I think it explains the behavior I'm seeing. It appears this was originally a Web Site Project NOT a Web Application. At any rate, as I noted in a comment above, I'm able to move the class files from the App_Code folder to the root and they compile fine. Thanks to everyone for their help!

0
votes

Add

using ErrorLogger; 

reference in your page.

0
votes

Add the folder to your using directives at the top of the implementing file, like so:

using AAT.App_Code;
0
votes

When you add the folder name App_Code, visual studio will treat the classes in this folder differently. The build action of the property of the classes is set as "Content". You need to click the property to change it as "Compile".