So I created a class file Persons to my website project and placed this in a folder called App_Code.
But now in my default.aspx.cs I cannot seem to create i.e. Persons test = new Persons();
Says
Type or Namespace Persons not found
This is my persons class so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.App_Code
{
public class Persons
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string DisplayFullName()
{
string info;
info = "FullName is: " + FirstName + " " + LastName;
return info;
}
public void setData(String sLine)
{
this.FirstName = "Test";
}
}
}
You need to import the appropriate Namespace in your Code behind(default.aspx.cs):
using WebApplication1.App_Code;
Your Person page and your aspx code page are in different namespaces. You cannot resolve type names across namespaces without importing them with the uses keyword.
Add
using WebApplication1.App_Code;
To the top of your default.aspx code behind page.
If you mouse over Person where it has the error, you should get a little helper popup that has the option of adding the missing using clause automatically!
If your working purely with an aspx file with no code behind, use:
<%# Import namespace="MyProgram.MyNamespace" %>
So in your case:
<%# Import namespace="WebApplication1.App_Code" %>
Related
i am writing common business logic class in App_code folder for connecting to database using EFW.but showing the following error " Error 1 The type or namespace name 'job' could not be found"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
namespace WebApplication6.App_Code
{
public class BlCommon
{
public List<job> GetJobs()
{
pubs2012Entities pubsobject = new pubs2012Entities();
var x = pubsobject.jobs.ToList<job>();
return x;
}
}
}
and class generated by EFW from jobs table is
namespace WebApplication6.App_Code
{
using System;
using System.Collections.Generic;
public partial class job
{
public short job_id { get; set; }
public string job_desc { get; set; }
public byte min_lvl { get; set; }
public byte max_lvl { get; set; }
}
}
Seem you dont have a reference to assembly wich contain mentioned class
Ok I got it:
Public List<job> GetJobs()
{
pubs2012Entities pubsobject = new pubs2012Entities();
var x = pubsobject.jobs.ToList<job>();
return x;
}
Click on Job, Move the mouse cursor over the blue dash that appear at the bottom of
job click Generate Class and then move your Job.cs code to your new job.cs class and
delete the old one.
I have a dbml that has stored procedures dragged off.I have EmployeeModel class that has get and set propertise .
I have an interface IEmployee and a Repository Employee Repository that has the implementation of the methods.Please refer the code.In Stored procedure GetRoles i just have SELECT * FROM ROLE .
In repository how to loop through the resultset.Can i change ISingleResult to IMultipleResult in dbml designer file?
EmployeeModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcWebsite.Models
{
public class EmployeeModel
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public string TaskMark { get; set; }
public int RoleFlag { get; set; }
}
}
IEmployee:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcWebsite.Models;
namespace MvcWebsite.DAL
{
public interface IEmployees
{
IList<EmployeeModel> ListAll();
// void Save(EmployeeModel employ);
}
}
EmployeeRepository.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcWebsite.Models;
using System.Data.Linq;
namespace MvcWebsite.DAL
{
public class EmployeeRepository:IEmployees
{
private DataDataContext _dataContext;
public EmployeeRepository()
{
_dataContext = new DataDataContext();
}
public IList<EmployeeModel> ListAll()
{
//IMultipleResults result =_dataContext.GetRoleDetails();
//var Emps = result.GetResult(EmployeeModel);
List<EmployeeModel> emp = _dataContext.GetRoleDetails();
// foreach (GetRoleDetailsResult role in Emps)
// {
// role.Description=Emps.
// }
return Emps.ToList();
}
}
}
You can loop through the resultset as below:
List<EmployeeModel> employeeModels = new List<EmployeeModel>();
foreach (EmployeeModel employeeModel in _dataContext.GetRoleDetails())
{
employeeModels.Add(employeeModel);
}
Or you can use System.Linq.Enumerable class ToList<> method as below:
List<Product> products = context.GetProducts().ToList<Product>();
IMultipleResults is used when stored procedure is returning more than one result sets. However when you drop such procedures on to the designer, it doesn't generate IMultipleResults. For this you can change the designer generated code to use IMultipleResults as below:
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetCustomerAndProducts")]
[ResultType(typeof(Customer))]
[ResultType(typeof(Product))]
public IMultipleResults GetCustomerAndProducts()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((IMultipleResults)(result.ReturnValue));
}
However, it would overwrite your modifications when you do any changes in the designer because it would regenerate the code. To avoid this you can use partial classes.
Or you can also use SqlMetal tool. It is a command-line tool that generates code and mapping for the LINQ to SQL component of the .NET Framework. This tool generates IMultipleResults. You can get the details for this tool here:
http://msdn.microsoft.com/en-us/library/bb386987.aspx
Edited:
Repository functionality will be same regardless of you work in ASP.Net Mvc or WinForms or any other presentation layer.
You can change your repository function to below:
public List<EmployeeModel> ListAll()
{
return _dataContext.GetRoleDetails().ToList<EmployeeModel>();
}
Or:
public List<EmployeeModel> ListAll()
{
List<EmployeeModel> employeeModels = new List<EmployeeModel>();
foreach (EmployeeModel employeeModel in _dataContext.GetRoleDetails())
{
employeeModels.Add(employeeModel);
}
return employeeModels;
}
This is my first time using EF in VS2012 as I have been using 2010 for up until now. I have added the entity framework model and it adds 2 files with the extension .tt which I am sure was not present in VS2010. Under one of these it generates partial classes to match the entities. However I already have these partial classes in another manually created folder called Entites under the root of my app. This causes an issue on build as they conflict...
How do I either either stop them autogenerating or how do I make them play nice with my manually created partial classes? It is incredibly annoying that VS2012 does this without asking as it breaks my code!
Example of Auto Generated class
namespace StatisticsServer
{
using System;
using System.Collections.Generic;
public partial class Statistic
{
public int StatID { get; set; }
public int CategoryID { get; set; }
public int FranchiseID { get; set; }
public double StatValue { get; set; }
}
}
Example of Manually created class
namespace StatisticsServer.Entities
{
public partial class Statistic
{
public static List<Statistic> GetStatisticsSet(int categoryID)
{
List<Statistic> statSet = new List<Statistic>();
using (var context = new StatisticsTestEntities())
{
statSet = (from s in context.Statistics where s.CategoryID == categoryID select s).ToList();
}
return statSet;
}
}
}
Make sure that your manually created classes are in the same namespace as the auto-generated ones.
Otherwise the two classes will be seen as separate partial classes, and if you use both namespaces in the same calling class it cannot determine which class you mean.
So for example in your case you might have:
using StatisticsServer;
using StatisticsServer.Entities;
When you then declare an object of the type Statistic in that class the build will fail because the Statistic class exists in both namespaces.
I've added to the activity pack the following activity:
namespace TeamFoundation.Build.ActivityPack
{
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Build.Client;
[BuildActivity(HostEnvironmentOption.Agent)]
public sealed class CheckSlothInitialized : CodeActivity
{
[RequiredArgument]
public InArgument<string> DbUser { get; set; }
[RequiredArgument]
public InArgument<string> DbPassword { get; set; }
[RequiredArgument]
public InArgument<string> DbServer { get; set; }
[RequiredArgument]
public InArgument<string> DbName { get; set; }
protected override void Execute(CodeActivityContext context)
{
string connString = String.Format(
"data source={0};Integrated Security=false;Initial Catalog={1};User ID={2};Password={3}",
DbServer, DbName, DbUser, DbPassword);
}
}
}
After that I compile it I can't find it in the toolbox. I'm going to choose items in the toolbox and choosing the dll of my activity, but even then I can't find it in the list of System.Activities Components.
Please follow these check points.
Clean and rebuild your solution.
If your activity is in independent project (activity library) and you are referring this activity in a project containg xaml or xamlx file make sure all referenced dll should be added in reference.
Try to add this activity as code behind in a workflow and execute it, is there any exception thrown?
I'm using Entity Framework Code First. Usually I have no problem, but for a database I'm working with I keep getting errors that it can't find the table in the SQL Server database. Here is what my class looks like:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class CustomApp_User
{
[Key]
public int UserID { get; set; }
[MaxLength(50)]
public string Username { get; set; }
[MaxLength(250)]
public string Email { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
}
In the database I have a table called "CustomApp_Users" to match the above. Note it has the "s" at the end.
And then I have:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
public class CustomAppDB : DbContext
{
public DbSet<CustomApp_User> CustomApp_Users { get; set; }
}
I expected EF codefirst to pluralize so that it would find "CustomApp_Users" in the database since this is how it usually works. But instead I get the error:
Invalid object name 'dbo.CustomApp_User'.
It appears it's not pluralizing the table name. I can't figure out why. One thing different with this database is that the Primary Keys do not follow the normal convention so I use the [Key] annotation.
I do know that if I use the [Table] annotation for my class it will work:
[Table("CustomApp_Users")]
But, I'd like to find out why the pluralization is not working the way I thought it would.
That is because the PluralizationService in EF can not pluralize it. It returns the same string if you pass it "CustomApp_User". Unfortunately you can not customize this service. So you need to configure the table name explicitly.