missing assembly reference - asp.net

CS0234: The type or namespace name 'ShoppingCart' does not exist in the namespace 'MyWebsite.Commerce' (are you missing an assembly reference?)
public class ProfileCommon : System.Web.Profile.ProfileBase {
public virtual MyWebsite.Commerce.ShoppingCart Cart {
get {
return ((MyWebsite.Commerce.ShoppingCart)(this.GetPropertyValue("Cart")));
}
set {
this.SetPropertyValue("Cart", value);
}
}
public virtual string Country {
get {
return ((string)(this.GetPropertyValue("Country")));
}
set {
this.SetPropertyValue("Country", value);
}
}
public virtual string Gender {
get {
return ((string)(this.GetPropertyValue("Gender")));
}
set {
this.SetPropertyValue("Gender", value);
}
}
public virtual int Age {
get {
return ((int)(this.GetPropertyValue("Age")));
}
set {
this.SetPropertyValue("Age", value);
}
}
public virtual ProfileCommon GetProfile(string username) {
return ((ProfileCommon)(ProfileBase.Create(username)));
}
}
when i run this page the an error occured and when i clicked the error line it goes to above code please anyone help me ASAP please.......

Check your project references - most likely the one that points to the assembly that contains MyWebsite.Commerce.ShoppingCart is missing. Is this a project reference? Perhaps you simply haven't built the solution yet.

Related

GetCustomAttribute always returns null

I have an attribute class
[AttributeUsage(AttributeTargets.Class)]
public class WizardAttribute : Attribute
{
public bool AddGuid { get; set; }
public WizardAttribute()
{
}
}
I'm trying to get this attribute via reflection like this
Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "bin\\some.dll");
Type[] types = assembly.GetTypes();
foreach (Type tt in types)
{
if (tt.Name == "Somecontroller")
{
WizardAttribute attribute = (WizardAttribute)(tt.GetCustomAttribute(typeof(WizardAttribute)));
}
}
and attributes always null
Please help
Ok I found it I change the loading assembly to
Assembly assembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "bin\\SwiftNess.Portal.dll");

Why are public properties being serialized w/ an underscore in MVC4?

In the following class
using System;
namespace Beverati.Repository.ViewModel
{
[Serializable]
public class CollectionItem : EditableEntityBase
{
public CollectionItem() {}
private int? _quantity;
private string _retailValue;
private string _currencyName;
public int? quantity
{
get { return this._quantity ?? NULL_INTEGER; }
set { this._quantity = value; }
}
public string retailValue
{
get { return this._retailValue ?? String.Empty; }
set { this._retailValue = value; }
}
public string currencyName
{
get { return this._currencyName ?? String.Empty; }
set { this._currencyName = value; }
}
}
}
returned in this controller action
public IEnumerable<Repository.ViewModel.CollectionItem> GetAll()
produces this JSON output with MVC2 the JSON output like this
{
quantity:2
retailValue: 50.00
currencyName: USD
}
However, when I installed MVC4 the JSON returned looks like this
{
_quantity:2
_retailValue: 50.00
_currencyName: USD
}
All the property names have an underscore prefix. Why is the underscore being used and what should be done to have the public property names returned in the JSON?
The [Serializable] attribute is what's doing it - Assuming you don't need it for another purpose - remove the attribute and all should be well.

Using design time repositories in MVVM Light and SimpleIOC

I have started to use the latest MVVM Light toolkit v4 (NuGet preview v4.1.21, DLL v: 4.0.21.25725), which implements the SimpleIOC pattern.
In my ViewModelLocator.cs, I have the following to handle both design and runtime repositories:
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<IWebRepository, DesignWebRepository>();
}
else
{
SimpleIoc.Default.Register<IWebRepository, WebRepository>();
}
SimpleIoc.Default.Register<MainViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
But this gives me the following Exception in the designer (so no compile errors, nor runtime errors, but I loose my Blendability) for the View:
Class Repository.IWebRepository is already registered
(at the first .Register() call)
IWebRepository:
public interface IWebRepository
{
string Test(string data);
}
DesignWebRepository:
public class DesignWebRepository : IWebRepository
{
public string Test(string data)
{
return "design result: " + data;
}
}
WebRepository:
public class WebRepository : IWebRepository
{
public string Test(string data)
{
return "result: " + data;
}
}
MainViewModel:
public class MainViewModel : ViewModelBase
{
IWebRepository webRepository;
public MainViewModel(IWebRepository webRepository)
{
this.webRepository = webRepository;
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
}
}
}
What am I missing here?
if (ViewModelBase.IsInDesignModeStatic)
{
if (!SimpleIoc.Default.IsRegistered<IRepository>())
{
SimpleIoc.Default.Register<IRepository, DesignWebRepository>();
}
}
else
{
SimpleIoc.Default.Register<IRepository, WebRepository>();
}
Notice the check for if the repository is already registered to prevent double registration. Hope this fixes your problem

Entity Framework Code First DBContext ObjectStateManager Error

I am getting an Error "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."
public class TestObject
{
public string Name {get;set;}
}
public EditTeamResponse Edit(TestObject testObject)
{
if (!ValidateTestObject(testObject))
{
return testObject;
}
try
{
_unitOfWork.TestObjectRepository.Update(testObject);
_unitOfWork.Commit();
}
catch (Exception)
{
//Error is thrown here
_validationDictionary.AddError("Unknown", "Unknown Error!");
return testObject;
}
// Other Extra Code
return editTeamResponse;
}
protected bool ValidateTestObject(TestObject testObject)
{
if (CheckIfNameChanged(teamToValidate))
{
if (_unitOfWork.TestObjectRepository.Any(x => x.Name == testObject.Name))
_validationDictionary.AddError("Name", "Name already exist.");
}
return _validationDictionary.IsValid;
}
private bool CheckIfNameChanged(TestObject testObject)
{
return _unitOfWork.TestObjectRepository.FindBy(testObject.TeamId).Name != testObject.Name;
}
I know that when I call CheckIfNameChanged(TestObject testObject) method i added an Entity Key to the ObjectContext and when i attach or edit the code when i call the _unitOfWork.TestObjectRepository.Update(testObject):
public void Update{
_context.Entry(entity).State = EntityState.Modified;
}
This is where the conflict happen and i got two same entity key in the ObjectStateManager. Is there a way to solve this problem without me going to the Context to detach the entity or is there some other way? And what is the best way to detach an entity from the context?
You can check if name changed as follows
private bool CheckIfNameChanged(TestObject testObject)
{
return !_unitOfWork.TestObjectRepository
.Any(x => x.TeamId == testObject.TeamId && x.Name == testObject.Name);
}

Using a session wrapper for replacing direct call to session variables

I use a session wrapper like this:
public interface ISessionWrapper
{
// ...
CultureInfo Culture { get; set; }
}
public class SessionWrapper: ISessionWrapper
{
private T GetFromSession<T>(string key)
{
return (T)HttpContext.Current.Session[key];
}
private void SetInSession(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
// ...
public CultureInfo Culture
{
get { return GetFromSession<CultureInfo>("Culture"); }
set { SetInSession("Culture", value); }
}
}
I can use this interface in my controller like this:
private readonly ISessionWrapper sessionWrapper = new SessionWrapper();
// ...
ci = new CultureInfo(langName);
sessionWrapper.Culture = ci;
But how can I access this wrapper in the view below to replace the (direct call to) session variable?
#switch (Session["Culture"].ToString())
{
case "fr":
// ...
case "uk":
// ...
}
You could make use of the base view:
public abstract class BaseViewPage : WebViewPage
{
public virtual ISessionWrapper SessionWrapper
{
get
{
return new SessionWrapper();
}
}
}
public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
{
public virtual ISessionWrapper SessionWrapper
{
get
{
return new SessionWrapper();
}
}
}
your views will have access to SessionWrapper property.
Make sure to add pageBaseType="SessionWrapper" attribute to pages tag in web.config.

Resources