In my web form project I opened a folder called App_Code inside that folder I have class named Test123, but when I try to create instance inside WebForm1.aspx.cs it doesn't recognize that type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace StephanProject_2.App_Code
{
public class Test123
{
}
}
My project structure:
I tried to add using StephanProject_2.App_Code;
What's the problem?
The class you are calling does not seem to be in the same namespace as the Webform1 class
if its from another namespace use.
StephanProject_2.App_Code.Test123 t = new StephanProject_2.App_Code.Test123()
Or alternatively in the top say
using StephanProject_2.App_Code;
When i try to same as your project then it works my code
You can see Here
I'm implementing a number of micrososervices with C# and Service Fabric. All has gone well until this error which seems to be a C# error:
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'IProductCatalogService' could not be found (are you missing a using directive or an assembly reference?) ECommerce.API C:\dev\ECommerce\ECommerce.API\Controllers\ProductsController.cs 14 Active
IProductCatalogService is the project ECommerce.ProductCatalog.Model which I have set a project dependency to and I am referencing with a using statement:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ECommerce.API.Model;
using Microsoft.AspNetCore.Mvc;
namespace ECommerce.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly IProductCatalogService _catalogService;
Here it is ECommerce.ProductCatalog.Model:
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.ServiceFabric.Services.Remoting;
namespace ECommerce.ProductCatalog.Model
{
public interface IProductCatalogService : IService
{
Task<IEnumerable<Product>> GetAllProducts();
Task AddProduct(Product product);
}
}
What am I missing?
From the screenshots above, it looks like you're missing out on referencing to the right project. You want to use using Ecommerce.Productcatalog.Model after you add the reference of Ecommerce.Productcatalog.Model in UI Project.
And from your comments it looks like you have done that and yet you're not able to add that reference. In that case, can you share the screenshot of what it says when you try adding that using statement and also share the project/folder structure if possible, so we can take a look it further.
I know that this question has been already asked, but I cannot find anything that can help solve my problem.
I want to connect my aspx page (Retete.aspx) to a Microsoft SQL database. I'm getting this error on Retete.aspx.cs:
Error 1 The type or namespace name 'GlobalClass' does not exist in the namespace 'ProiectSera' (are you missing an assembly reference?)
The error points to this line of code:
ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
Where ProiectSera is the project name, GlobalClass is the file where I make the operation on the db.
My using statements are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
The Target Framework is set to .net-4.5.
What should I do to solve this error?
Update
My GlobalClass.cs is:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using ProiectSera;
using System.Data.Services;
namespace ProiectSera.App_Code
{
public static class GlobalClass
{
static public void Update(string _param1, string _param2)
{//function to update}
}
}
App_Code is a folder where GlobalClass.cs is. I tried and
ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text); //
but I had the same error. And I put the GlobalClass.cs in the project's root. I also removed the .App_Code from namespace ProiectSera.App_Code
UPDATE1
My Retete.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
using ProiectSera.App_Code;
namespace ProiectSera
{
public partial class Retete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
string intValRefTempSol;
string intValRefTempAer;
// intValRefTempSol = ValRefTempSol.Text;
// intValRefTempAer = ValRefTempAer.Text;
// ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
}
}
}
Your GlobalClass is in the namespace ProiectSera.App_Code.
So the class name is ProiectSera.App_Code.GlobalClass
Make sure you don't have a ProiectSera class in the ProiectSera namespace also, otherwise if declaring using ProiectSera on top, it'll try to use it (as a general rule, don't name any class the same as your namespace).
If that still doesn't work, you may want to try using the global namespace:
global::ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
and see if that works: if it doesn't, and GlobalClass is in the same project, then there's something else you haven't shown us
Update
The only other thing that comes to mind, if you are positive that both files are on the same project/assembly, is that GlobalClass.cs is not being actually compiled. Make sure the Build Action is set to Compile (you can see the build action right clicking on the GlobalClass.cs in the solution explorer and selecting Properties).
If you are using VS.NET:
Right click on the References folder on your project.
Select Add Reference.
Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
Double-click the assembly containing the namespace in the error message.
Press the OK button.
Ensure the following...
That you have a project reference to ProiectSera from your web application, if it's in a separate library. If GlobalClass is in the web application project itself, you won't require this, but if GlobalClass is defined in a separate library in the same solution or elsewhere, then you're going to need to add a reference to that external library in your web application.
Ensure that ProiectSera should not be ProjectSera (if it's a typo).
Ensure that you have your ProiectSera using statement in place at the top (using ProiectSera;). Assuming that this is the correct namespace. Check the namespace of your GlobalClass class and use that using accordingly in the class that wishes to use its functionality.
Then, simply call this in your code, assuming that Update is a static method of GlobalClass.
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
EDIT: given that you've now posted your GlobalClass code, you can see that the namespace for this class is ProiectSera.App_Code;. So you need to have that as your using statement in the other class using ProiectSera.App_Code;. And then call it via simply GlobalClass.Update, as mentioned above.
If you don't understand how namespacing works in C#, then I recommend you check this out (https://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx).
I have no idea. I just created a Global.asax and I'm just trying to use System.Data.Entity.Database and it has no idea what I want.
using System;
using HROpenEnrollment.Model;
using System.Data.Entity;
using HROpenEnrollment.Data.EntityFramework;
namespace HROpenEnrollment
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start()
{
using System.Data.Entity.
}
The irritating part is I can use it elsewhere. Here's another file in the same project that works fine.
using System;
using HROpenEnrollment.Model;
using System.Data.Entity;
using HROpenEnrollment.Data.EntityFramework;
namespace HROpenEnrollment.Data.EntityFramework
{
public class Populate : DropCreateDatabaseIfModelChanges<OpenEnrollmentContext>
{
What gives? I get all the way to System.Data.Entity but it won't find anything after that.
Have you installed Entity Framework 5? Having only version 4 of the System.Data.Entity dll will net you nothing.
I have been wondering what is better way of using code.
Is there any difference between writing using inside of namespace:
ouside of namespace
using System.Data.Entity;
using System.Web.Mvc;
using System.Web.Routing;
using Myproject.DataLayer;
namespace Myproject
{
public class MyProjectClass{
// etc
}
}
Inside of namespace
the only reason i can thing of : shorter names, and the complier does not need to go to global location to find the implementation... but not sure about the rest
namespace Myproject
{
using System.Data.Entity;
using System.Web.Mvc;
using System.Web.Routing;
using DataLayer;
public class MyProjectClass{
// etc
}
}
Scott have already answered the question at http://www.hanselman.com/blog/BackToBasicsDoNamespaceUsingDirectivesAffectAssemblyLoading.aspx
Value of keeping the using code & Alias names inside namespace incase if you decide to have multiple namespaces in one file.
But the question to ask yourself is why do I want to have multiple namespace's in the same file