Why a class in App_Code/BLL is not accessible in pages? - c#

Hi I have a class in folder App_Code/BLL/Countries.cs ,which is a public class.
But when I tried to access the class in a ASP.NET page like:
Countries MyCountries = new Countries();
its showing error:
"The type or namespace name 'Countries' could not be found (are you missing a using directive or an assembly reference?)"

Ok your countries class will look something like this
namespace BGridNDynamicPage1
{
public class Countries()
{
......
}
}
and your code above will be something like this:
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
change your second class too:
using BGridNDynamicPage1;
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
See http://msdn.microsoft.com/en-us/library/sf0df423.aspx

Related

CS0246 error while trying to create xUnit tests

I'm not even trying to test anything there yet, just wanted to create an instance of class Book(that I would like to test in the future) in my testing BookTests.cs file. I've added reference to GradeBook.Tests.csproj as whole source code to Gradebook is in the other folder. Yet it still returns CS0246 error : The type or namespace name 'Book' Could not be found.
Are you missing a using directive of assembly reference?
Gradebook.Tests.csproj with directory tree
using System.Collections.Generic;
namespace Gradebook
{
public class Book
{
private List<double> grades;
private string name;
public Book(string name)
{
this.name = name;
grades = new List<double>();
}
public void AddGrade(double grade)
{
grades.Add(grade);
}
}
}
Book.cs
using System;
using Xunit;
namespace GradeBook.Tests
{
public class BookTests
{
[Fact]
public void Test1()
{
//arrange
var book2 = new Book("test");
}
}
}
BookTests.cs
The code is pretty simple as I'm following Pluralsight course C# Fundamentals and I did everything like on the video. Tried also dotnet restore command. Path to project that contains Book.cs is probably correct(when i change something other errors occur). Working on VSC. What am I missing here?
I know it's been 2 months since this has last had an update, but the reason you're having an issue is because you're using the namespace Gradebook in Book.cs, but using GradeBook in BookTests.cs. (Notice the capital B)
Rename it so they're both Gradebook and ensure your directory names are correct and you should have no issues.

Getting error in Asp.net core when implementing TodoList service

I'm following on some tutorial which creates TodoList and uses multiple layer structure to handle different business logic. When I copy code from the book it shows me an error when I fire dotnet run command in windows cmd. The error I get is displayed bellow so I can specify where lies the problem but don't know how to fix this. What can I do to make this work ? I'm using dotnet 3.0.100 version.
Services\FakeTodoItemService.cs(21,17): error CS0117: 'FakeTodoItemService' does
not contain a definition for 'Title' [C:\dotnetproject\AspNetCoreTodo\AspNetCor
eTodo\AspNetCoreTodo.csproj]Services\FakeTodoItemService.cs(22,17): error CS0117: 'FakeTodoItemService' doesn not contain a definition for 'DueAt' [C:\dotnetproject\AspNetCoreTodo\AspNetCor
eTodo\AspNetCoreTodo.csproj]
And here is a code which I think may be responsible for this error. Basically I have a service that implements interface which uses a model.
FakeTodoItemService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspNetCoreTodo.Data;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Services
{
public class FakeTodoItemService : ITodoItemService
{
public Task<TodoItem[]> GetIncompleteItemsAsync()
{
var item1 = new TodoItem
{
Title = "Learn ASP.NET Core",
DueAt = DateTimeOffset.Now.AddDays(1)
};
var item2 = new FakeTodoItemService
{
Title = "Build awesome apps",
DueAt = DateTimeOffset.Now.AddDays(2)
};
return Task.FromResult(new[] {item1, item2});
}
}
}
TodoItem.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace AspNetCoreTodo.Models
{
public class TodoItem
{
public Guid Id {get; set;}
public bool IsDone {get; set;}
[Required]
public string Title {get; set;}
public DateTimeOffset? DueAt {get; set;}
}
}
ITodoItemService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Services
{
public interface ITodoItemService
{
Task<TodoItem[]> GetIncompleteItemsAsync();
}
}
In FakeTodoItemService.GetIncompleteItemsAsync you have this line var item2 = new FakeTodoItemService. FakeTodoItemService should be TodoItem.

How to make a field unique ASP.NET MVC5

I want to make Email field unique. It's a field of ApplicationUser (of IdentityUser) That's what I did :
namespace BidProject.Models
{
public class ApplicationUser : IdentityUser
{
public int UserID { get; set; }
[Index(IsUnique = true)]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I have these references:
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
I get these errors for that :
The type or namespace name 'Index' could not be found (are you missing
a using directive or an assembly reference?)
The type or namespace
name 'IndexAttribute' could not be found (are you missing a using
directive or an assembly reference?)
How can I make that field unique? Or can you tell me how IdentityUser's UserName field is unique? Thanks.
Add this:
using System.ComponentModel.DataAnnotations.Schema;
If that doesn't work, check if you have a reference to EntityFramework.dll.
You should specify a name for the Index. Use the below syntax.
[Index("IX_EmailUnique", 1, IsUnique = true)]
Looks like you are missing a namespace at the top of the file, or a project reference to EntityFramework.dll? Try putting your cursor inside the word 'Index', then hit ctrl-. -- you should see a popup menu offering something like 'resolve reference' or 'add using for using System.ComponentModel.DataAnnotations.Schema;'
If you've got, say, a .net 4.0 project referencing a .net 4.5 assembly, sometimes that'll break things. You'll need to upgrade the project to a more recent version of the framework.

Migrations.cs is not created successfully

I am creating new module called "sms" in orchard cms using webmatrix. I create it successfully but when i generate "migrateions.cs", it doesn't generated successfully.
my sms.cs class in Model is given below
using System.Collections.Generic;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
namespace SMSs.Model{
public class smsrecord:ContentPartRecord
{
public virtual int ID{get;set;}
public virtual string Name{get;set;}
public virtual char is_deleted{get;set;}
}
public class smspart:ContentPart<smsrecord>
{
[Required]
public int ID
{
get{return ID=Record.ID;}
set{Record.ID=value;}
}
public string Name
{
get{return Name=Record.Name;}
set{Record.Name=value;}
}
public char is_deleted
{
get{return is_deleted=Record.is_deleted;}
set{Record.is_deleted=value;}
}
}
and the generated Migrations.cs class is as follow
using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
namespace sms {
public class Migrations : DataMigrationImpl {
public int Create() {
return 1;
}
}
}
the "migrations.cs" is not generated successfully why?? Please help
Class itself is generated properly, although it lacks code for creating appropriate tables because you didn't adhere to naming conventions for your record class.
Data migration code generation requires you to follow several conventions in order for it to work properly. I.e.:
Namespace of a record must end with .Models or .Records
There has to exist a public property named Id
All properties have to be virtual (required by NHibernate anyway)
Class cannot be sealed
Class cannot be abstract
Class has to implement IContent or be a subclass of ContentPartRecord
In your case, the namespace (should end with .Models) and incorrect casing of ID (should be Id) are the culprits.

Nancy Model Binding

Hi I'm learning Nancy and I'm trying to bind to a model, but I'm getting the error:
Error 8 'NancyFxTutorial.CarModule' does not contain a definition for 'Bind' and no extension method 'Bind' accepting a first argument of type 'NancyFxTutorial.CarModule' could be found (are you missing a using directive or an assembly reference?) C:\Development\Projects\C#\Web\Nancy\NancyFxTutorial\NancyFxTutorial\CarModule.cs
model:
public class BrowseCarQuery
{
public string Make { get; set; }
public string Model { get; set; }
}
public class CarModule : NancyModule
{
public CarModule()
{
Get["/status"] = _ => "Hello World";
Get["/Car/{id}"] = parameters =>
{
int id = parameters.id;
return Negotiate.WithStatusCode(HttpStatusCode.OK).WithModel(id);
};
Get["/{make}/{model}"] = parameters =>
{
BrowseCarQuery model = new BrowseCarQuery();
var carQuery = this.Bind<>()
};
}
}
any clues?
Thanks in advance
The Nancy model binding methods are defined as extension methods on the NancyModule class.
And these extension methods can be found in the Nancy.ModelBinding namespace.
So you need to using the Nancy.ModelBinding namespace to access the Bind() and BindTo() methods.
So add this line to your source file:
using Nancy.ModelBinding;

Categories

Resources