Solved

Visual Studio 2015 - Had to load an existing ASP.NET c# project into a new solution now getting CS0103 errors all over the place.

Posted on 2016-08-21
15
73 Views
Last Modified: 2016-08-22
Hi,
I had a web project that compiled without issue. Strangely that project didn't have a solution file. I then had to do a clean install of my PC and as part of the tidying up process after that I decided to have a tidy up and rebuild this project but this time I wanted to have a solution file.

I copied the old project to a temporary folder and then created a new empty web solution with exactly the same name and in the same folder.  I then imported the files I wanted to keep back into the new project using the "Add existing item.. " method.  I had all the files I wanted and the project was all set to go. I added back the same references and added all the ASP folders like APP_Code and App_Data as well as custom folders like "font", "images", "css" and "js".

Everything looked as it did in the original project so I tried to compile it and when I did I get masses of CS0103 errors.  The initial ones all relate to classes in my App_Code.  I have a class called "Common" that I use to keep common routines in.  Everywhere in my code where I have put something like this:

            Common.DBFilePath = Server.MapPath("App_Data\\Kohola.mdb");
            paramCN = new OleDbConnection(Common.DefCon + Common.DBFilePath);
            paramCN.Open();

The emboldened items are underlined as an error and there is a corresponding CS0103 error saying:


The name 'Common' does not exist in the current context      Dashboard

Well it does and it's in my APP_Code folder, the Common Class has this at the top of it:

using System;

namespace Dashboard
{
    public static class Common
    {
        ..... various functions that I use all over the place.
    }
}

Where am I going wrong? How do I get the project to recognise that wherever I use common I am referring to my Common Class that is within the same project?

Any help appreciated, this is driving me nuts!

Siv
0
Comment
Question by:Siv
  • 7
  • 6
  • 2
15 Comments
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 41764821
Right click the cs file in the app code folder and check it's properties.

Make sure that the build action is set to compile.

I've had that problem before under similar circumstances and that did the trick for me.

Hope this help,
Dustin
0
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41764897
For website project, it is not needed to have solution file! So from your old website project it would not have! Solution file is just the collection of multiple projects in metadata format (XML).

Check with namespace, or recreate common file and put the code into it!

If in your project folder "project.json" file exists, then follow steps mention from below link:
https://social.msdn.microsoft.com/Forums/silverlight/en-US/98dbf3ca-3efd-4649-85f6-6d6af852f6de/error-cs0103-the-name-file-does-not-exist-in-the-current-context?forum=csharpgeneral
0
 

Author Comment

by:Siv
ID: 41765327
Dustin,
I changed all the class modules in my App_Code folder to compile as there are a lot of other classes that map to database tables that I use all over the place and it seemed to do the trick and then about 10 seconds later the errors all came back.  

I am now getting 447 errors that now seem to be mainly CS0246 and as follows:

Severity      Code      Description      Project      File      Line      Suppression State
Error      CS0246      The type or namespace name 'GraphValues' could not be found (are you missing a using directive or an assembly reference?)      Dashboard      D:\Dev\Kohola\Dashboard\frm7-06.aspx.cs      90      Active

GraphValues is one of my class modules that maps to a database table and I am also getting this for all the other Class modules that are in App_Code everywhere I have used that class in my code??


Now I am also getting 194 warnings the following example being representative:

Severity      Code      Description      Project      File      Line      Suppression State
Warning      CS0436      The type 'DataExtractTable' in 'D:\Dev\Kohola\Dashboard\App_Code\DataExtractTable.cs' conflicts with the imported type 'DataExtractTable' in 'Dashboard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'D:\Dev\Kohola\Dashboard\App_Code\DataExtractTable.cs'.      Dashboard      D:\Dev\Kohola\Dashboard\App_Code\DataExtractTable.cs      187      Active

I am really confused now?
0
 

Author Comment

by:Siv
ID: 41765332
Prakash,

Can you explain how that works, is there just CSProj file when you do it as a website?

I don't have a project.json file in this project as far as I can see where would it be?

Siv
0
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41765367
As I already explained, solution file is the just the meta data of projects collection.
Solution file is just the collection of multiple projects in metadata format (XML).

Possibility one: This kind of issues happens due to incorrect namespace like your project has namespace "myNamespace" and your imported files has namespace like "myNameSpaces" etc.
Possibility two: You might have imported "BIN" folder as well! (think), if so please remove that one!
Possibility three: Your temp folder has the same folder in which it has (copy of) required files, Please remove/clear TEMO folder!

then, try with "Clean Solution" and then "Build Solution"!
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 41765443
First, make sure that your project doesn't have a reference to it self, if would show up under references. Also double check that the namespaces do match like Prakash suggested.

Second and this is likely, I believe you actually had a website project and are trying to convert it to a web application project. You should follow the steps here https://msdn.microsoft.com/en-us/library/aa983476.aspx

Specifically the "Converting the Project Files" section regarding the app_code directory.

"Because ASP.NET dynamically compiles all classes in the App_Code folder, you should not store classes in the folder that you compile as part of your Web application project. If you do, the class will be compiled two times. The first time will be as part of the Visual Studio Web application project assembly, and the second time will be at run time by ASP.NET. This can cause a "could not load type" exception, which occurs because there are duplicate type names in the application.
Instead, store class files in any folder of your project other than the App_Code folder. This is done automatically by the Convert to Web Application command, which renames the folder to Old_App_Code."

Hope this helps,
Dustin
0
 

Author Comment

by:Siv
ID: 41765531
Dustin,

I actually want it to be a website project but when I started to re-create it the only project template that was for C# seemed to be a web application? Maybe I need to start over and create a new project again and try and end up with a web site again?

Would you mind pointing me in the right direction as to how to go about this as I am floundering a bit now and think I am making a bad situation worse.

Siv
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 41765582
Ok,
1. in visual studio select New Project. Choose blank solution as the type and create a directory. Name it whatever you want your solution, not project, to be called.

2. Now that you have a solution, copy your original working website folder into the solution folder. So your structure is SOURCEDIR/SOLUTIONDIR/WEBSITEDIR.

3. Once you have copied the original folder, right click the solution name in the solution explorer. Choose Add->Existing Website. Then choose the WEBSITEDIR you created above.

NOTE: if you already have a solution that you want to make the site part of, then just copy the original site to that soltuion's folder and follow step 3.

That should allow you to have a solution file and use the website project instead of web application project.

Hope this helps,
Dustin
0
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 500 total points
ID: 41765598
Also the reason you didn't see the website option is because a website is not technically a project (we just call it that to reduce ambiguity). You can create a new "website" by clicking File->new->website or using the add option under the solution context menu.

You should probably check out https://msdn.microsoft.com/en-us/library/dd547590(v=vs.110).aspx to see all of the differences and when to use each type of project.
1
 

Author Comment

by:Siv
ID: 41765652
Dustin,
Thanks for all your help.  I have gone down the File >> New >> Website option as I am sure that is what I did originally now that you mention it.

I have added the .aspx and .cs files to it and all the custom folders like css, font, images etc. and it seems to have zero errors.  Then I ran the project and then the errors returned.  It seems to be that I still get the issue where my App_Code files are not being seen as intrinsic to the project so I am having to add a "using Dashboard;" to the top of each code behind file to get rid of the errors?

Is there something else I am missing.  It's as if the individual code behind files don't know the App_Code class files are part of the project??

I am reading the link you sent me on MSDN.
Siv
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 41765673
Just to be clear, adding the using statement to the codebehind fixed the previous issues. With the exception that you have to modify each page with a using statement.

This makes sense because in a Website the classes in app_code usually don't have a namespace if you want them to be part of your root project.

Just remove the namespace portion of your app_code classes if they match the project name and were being used without a using statement. May want to verify on a couple before doing them all.

Hope this helps,
Dustin
1
 

Author Comment

by:Siv
ID: 41765695
Dustin,

That's it, all the App_Code files had namespace Dashboard{} in them so I removed them and then remove all the "usings" I had put in to each .cs file and we are now back in business!

Thanks for your help, the key issue was that I had tried to turn it into a web application when what I really needed was a website project.  With your help I have got my project back up and running and also my overall aim of tidying it up and updating my DevExpress controls to the new 16.1.5.0 version have now been achieved.

I will be closing the question and allocating you the points.

Do you work at MS on Visual Studio the way you referred to (we just call it that to reduce ambiguity) sounded like you might be in that area?  Just curious.

Again thanks very much!

Siv
0
 

Author Closing Comment

by:Siv
ID: 41765700
Thanks very much you have helped me get my asp.net website back up.
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 41765726
Siv,
I'm glad you got everything working again.

I do not work for Microsoft, I've just been doing this a while and most developers I've met call it a website project, because just saying website could get confusing and saying Visual Studio Website is too long. Though it's not technically a Visual Studio Project.

VS Websites are not as popular as they used to be since they aren't as friendly with source control and unit testing, since someone could just edit the un-compiled file after it's been published to the server.

Thanks,
Dustin
0
 

Author Comment

by:Siv
ID: 41765932
Cheers Dustin and thanks again!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now