DataClassesDataContext Not Found - VS2012 - c#

I'm doing a project in ASP.NET and I'm using Visual Studio 2012 with .NET 4.5.
I've been searching online for my problem but haven't found any solution yet. Maybe anyone here can help me.
I'm working on a project and started with an empty Web Application. I've added a folder "App_Code" in which I put my Models and DataClasses. I've added a LINQ-to-SQL class with the correct tables and saved this file in the App_Code folder.
Now, when I add a Class to the App_Code folder, the class can't find the DataClasses.
This is the code of my class "Product" in the folder App_Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Outside.App_Code
{
public partial class Product
{
private static OutsideDataClassesDataContext db;
public static List<Product> GetAll(int id)
{
// Init the DB
db = new OutsideDataClassesDataContext();
// Get all the products and return
return db.Products.Where(a => a.id > 0).ToList();
}
}
}
Visual Studio won't let me run the code. I get this error:
The type or namespace name 'OutsideDataClassesDataContext' could not be found (are you missing a using directive or an assembly reference?) \App_Code\Product.cs 10 24 Outside
The build action of the file Product.cs is "Compile".
Can anyone help or give advice about this? Or try to help get the DataClasses recognised? I'm still learning ASP.NET. If you need other data or other code, just tell me.
Thanks in advance!

I've removed everything from the folder App_Code and made a new folder called Models. Placed everything (class Product and a new OutsideDataClass) in the folder Models and suddenly it works. Weird.
App_Code seems like a bad idea.

Firstly, make sure you have Linq to SQL added manually from the Visual Studio installer.
It will be under 'Individual Components'
The error is looking for the Linq dbml file that makes a generic version of the database you are connected to, to add this dbml right-click on your solution/project;
Choose Add New Item, then under Data, choose Linq-To-SQL Classes . This will create an empty DBML file. You'll then need to create a server connection to a database (if you have not already) and drag the tables you want into the DBML designer.

Looks like your OutsideDataClassesDataContext() is
in another namespace or in another project of your solution.
Try to shorten your namespace name to Outside
or add reference from project, where your class exists.

Related

Entity Framework 6.20, keep having to change System.Data.Objects to System.Data.Entity.Core.Objects;

I'm using the Entity Framework 6.20 with c#, Visual Studio 2017.
Whenever I update my object model from the database in my Model.Context.cs my lines:
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Core.Objects.DataClasses;
automatically change to
using System.Data.Objects;
using System.Data.Objects.DataClasses;
then I get many errors saying:
The type or namespace name 'Objects' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
So I just need to change the two lines back to how they were, but why is this happening? Can I stop it happening?
Thanks - Ben
Maybe this is because your .tt are outdated or you have had a update/migration from one entity framework version to another
The Model.Context.cs is automatically generated, so you can edit it you Model.Context.tt (this is a T4 template used to generated code automatically)
in solution explorer open the file Model.Context.tt
Then search the using lines that given you the errors , modify with the correct values and save the file, later right clik over this file and select Run Custom Tool.
I'll hope that this will be the solution, regards

Type or namespace name 'xxx' could not be found in Umbraco

When I created a class, there is no existing namespace declaration and I need the namespace declaration to access the class from a view (cshtml).
Normally when you create a class, there should be a namespace declaration included right? But for me I don't see any.
I already tried to add a namespace declaration, but I still can't access it from another class.
I hope you could help me with this.
Screenshots: https://drive.google.com/open?id=0B5TGcf9mTeg2V2ZDaldWelp6WXc
ScreenShot
make sure your namespace name is correct .using PRL(Capitalized) .Models .And the good practice is add an empty web application project ,not a website.
Just as what Nico Said (https://stackoverflow.com/users/5685258/nico)
Follow this instructions and this will be resolved:
https://our.umbraco.org/documentation/Getting-Started/Setup/Install/install-umbraco-with-nuget
Although I started all over again.
~I created a new project from Visual Studio 2017 using ASP.NET Web Application
~Selected an empty file
~ Right click on the project
~ Click Manage NuGet Packages and installed Umbraco CMS
~ Run it from VS and install the umbraco platform using the browser
~ As soon as I create a class, a namespace declaration will also be included.
Thanks!
Your model belongs to namespace PRL.Models but you try using Prl.Models - namespaces are case-sensitive. Try using PRL.Models
Edit: and for the class without namespace-declaration: you should always have a class in a namespace. Declare one.

Compiler Error Message: CS0246 [duplicate]

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.

ASHX including code-behind class -

I'm trying to use an external code file to include some helper classes for several .ashx handlers. The example one is using ListToJSON, which just turns nested Lists (Theres stuff like List<List<whatevers>> that are being worked with) into JSON (which will get thrown into context.response)
The ListToJSON class works fine when it's in the same file. I'm trying to put it into a Helper.cs file which is included in the same project in VS2010, because these classes get used in several different handlers.
I was under the impression that "using Helper;" was what I needed to do, but I still get the error "The type or namespace Helper could not be found (are you missing a directive or assembly reference?"
(and intellisense doesn't see it either)
I've also tried putting both code files into the same namespace. Same error.
It's not a DLL file, it's just a c# code file in the same project. Should I realy be compiling it as a DLL to do this? If so, how do I do that? (Once I do, I can do the right click->add reference, correct?)
I think I'm supposed to be using the App_Code folder, but I'm not really sure how to set that up in VS so that it's referenced properly.
My handler file (skeleton)
<%# WebHandler Language="C#" Class="generateReport" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;
using Helper;
public class generateReport : IHttpHandler {
public void ProcessRequest (HttpContext context) {
ListToJSON Converter = new ListToJSON();
//context.Response stuff goes here.
}
}
My helper file, Helper.cs - this is in the same project directory and is included in the project:
using <stuff>;
namespace Helper
{
public class ListToJSON
{
//class definition here
}
}
UPDATE: So, I put Helper.cs into the /App_Code/ folder, and it seemed to be playing nicely. Intellisense was picking up things in Helper.cs after I did using Helper;
When I tried it on IIS, it I got the following familiar error:
Compiler Error Message: CS0246: The type or namespace name 'Helper' could not be found (are you missing a using directive or an assembly reference?)
Line 19: using Helper;
Source File: <path>\info.ashx Line: 19
I get no errors when running this through visual studio's IIS emulator. When I run it through IIS (localhost) I get the IIS internal server error described.
App_Code folder was made through VS, I right clicked and chose Add ASP.NET Folder > App Code\
Edit: Added tag iis
I assume that you are using WebSite and not WebApplication.
While adding new Class file in the Website, It should be in the App_Code Folder to avoid the Accessibility issues.
Edit - 1 => Please see below the Publish details.
Edit = 2 => Try adding the individual Assembly in the bin folder as stated below. This will confirm that the App_Code folder Dll are incorporated.
For IIS to read classes correctly from the App_Code folder, the App_Code folder (or the bin folder, for compiled assemblies) needs to be in the IIS root directory in order for classes within to be detected.
In my case, I was publishing to a subfolder of the IIS root directory, and so IIS couldn't see the classes in it.

System.Windows.Forms.DataVisualization Namespace Fine in One Class but Not in Another

I'm getting this error
The type or namespace name 'DataVisualization' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)
Here is my using section of the class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms.DataVisualization.Charting.Borders3D;
using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
using System.Windows.Forms.DataVisualization.Charting.Data;
using System.Windows.Forms.DataVisualization.Charting.Formulas;
using System.Windows.Forms.DataVisualization.Charting.Utilities;
namespace myNamespace {
public class myClass {
// Usual class stuff
}
}
The thing is that I am using the same DataVisualization includes in another class. The only thing that I can think that is different is that the classes that are giving this missing namespace error are Solution Items rather than specific to a project. The projects reference them by link. Anyone have thoughts on what the problem is? I've installed the chart component, .Net 3.5 SP1, and the Chart Add-in for Visual Studio 2008.
UPDATE: I moved the items from Solution Items to be regular members of my project and I'm still seeing the same behavior.
UPDATE 2: Removing the items from the Solution Items and placing them under my project worked. Another project was still referencing the files which is why I didn't think it worked previously. I'm still curious, though, why I couldn't use the namespace when the classes were Solution Items but moving them underneath a project (with no modifications, mind you) instantly made them recognizable. :\
You are very likely missing a reference to the DataVisualization DLL. Note that although they share the namespace of System.Windows.Forms.dll, they aren't actually contained within it.
Solution items aren't used by compiled assemblies.
http://msdn.microsoft.com/en-us/library/1ee8zw5t.aspx
"They can be referenced by projects, but are never included in solution or project builds"
As far as I know, solution folders/items are really just meant for organizing things.
Are you getting actual build errors or just squiggles? Try building and look at the output window, does it succeed or fail?
In VS 2008 SP1 C# introduced a top level error squiggling feature. It's possible that if you open the solution item version of the file it will squiggle because of a lack of default references. The solution should still build correctly though.
If this is not the case try adding the file directly to the project (no link). See if that eliminates the error. If so then we know it has to due with a linked file and it can help track down the problem.

Categories

Resources