Cannot access my properties of a class in sharepoint - sharepoint

I have a problem with my Sharepoint application. I started programming it as a normal project and now I like to “move” it into a Sharepoint application. My app hast two projects one is the actual project for sharepoint and one contains the classes. I signed my classes-project, it has a strong name and is referenced. So far so good… But now I have the problem that I cannot access the public methods within my class anymore. So when instantiate my class:
var myName = new ClassName();
and later try to access it, for example:
myName.Size = 20;
“Size” will be marked red with the error “cannot resolve symbol”. But before putting it all into a sharepoint app it worked just fine! I googeld up and down but couldn’t find a solution for this problem.
Does someone know this problem?
UPDATE:
This is my code in the main project:
protected override void CreateChildControls()
{
...
HPlaner hp = new HPlaner();
hp.Entries.Add(new HPlanerEntry("Micky Mouse", new DateTime(2012, 12, 24), new DateTime(2013, 1, 13)));
hp.Scale = 2;
hp.Year = year;
hp.Summary = true;
this.Controls.Add(hp);
}
and this is the class:
public class HPlaner : WebControl
{
private List<HPlanerEntry> _Entries;
public List<HPlanerEntry> Entries
{
get
{
if (_Entries == null)
{
_Entries = new List<HPlanerEntry>();
}
return _Entries;
}
}
public bool Summary { get; set; }
public int Scale { get; set; }
public int DayCount { get; set; }
public int UserCount { get; set; }
public int Year { get; set; }
public string[,] UserList { get; set; }
... (myMethods)
}

My guess is that your code is written targeting x86-architecture while the SharePoint-code target 64-bit. You can change the setting of your code by right-clicking your project in VS and select Properties->Build->Platform Target. Select x64 and recompile.

Sorry guys, the answer was simple! The Main-Class in the normal project was _Default and in the Sharpoint Application HPlaner, which is the same name as my other class. You could not see this, since the class-name was not inluced in my listing. How timeconsuming can "little" things like that be!!

Related

C# ConcurrentDictonary Setting values in collection other then the key

Please look through my code before closing it this time.
The code below works but seems very hacked, I am looking for suggestions on to achieve the same thing with cleaner code or is this as good as it gets.
The code calling the Add and Remove will be from different threads that could possible access the code at the same time, so it must remain thread-safe.
using System;
using System.Collections.Concurrent;
namespace Server
{
public class Company
{
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public ConcurrentDictionary<string, Employee> Employees = new ConcurrentDictionary<string, Employee>();
}
public class Employee
{
public string First { get; set; }
public string Last { get; set; }
public string Ext { get; set; }
}
public class Clients
{
public ConcurrentDictionary<string, Company> CompaniesDict = new ConcurrentDictionary<string, Company>();
public bool Add_Company(string ID, string Name, string Address, string Phone) //This function works
{
Company MyCompany = new Company();
Employee MyEmployees = new Employee();
MyCompany.Name = Name;
MyCompany.Address = Address;
MyCompany.Phone = Phone;
MyCompany.Employees = MyEmployees;
return CompaniesDict.TryAdd(ID, MyCompany);
}
public bool Remove_Company(string ID) //This function works
{
return CompaniesDict.TryRemove(ID, Company tCompany);
}
//This is were I need the help this seems so hacked. Im not trying to update the key, but the value intstead
public bool Set_CompanyName(string ID, string Name)
{
CompaniesDict.TryGetValue(ID, out Company oCompany);
Company nCompany;
nCompany = oCompany;
nCompany.Name = Name;
return CompaniesDict.TryUpdate(ID, nCompany, oCompany);
}
public string Get_CompanyName(string ID)
{
CompaniesDict.TryGetValue(ID, out Company tCompany);
return tCompany.Name;
}
}
}
Please don't just close this and link me to some useless code you call a duplicate. Sorry to be so blunt but this has recently happened to me by a fellow coder on this site. If you have questions that I can answer so that you can full help me please ask them.
Thanks for you help in advance.
There is a much easier approach, as you are updating a field on an object.
Please note that I don't have C# installed on my current PC, so can't validate this.
Note that I declare the out parameter, but don't construct a new one that would be destroyed immediately and I modify the object itself.
i.e.
Company company;
not
Company company=new Company();
This will still not be deterministic if multiple threads call SetCompanyName(), as the new name is updated on the live object and there could be a potential race condition. However the Add and Remove will be, even if Remove removes the company instance just before its name is updated.
public bool Set_CompanyName(string ID, string Name)
{
Company company;
var retval= CompaniesDict.TryGetValue(ID, out company)
if (retval) {
company.Name=Name; // Update your company object directly
}
//else Do something such as throw an exception if it's not found
return retval;
}

Converting a application user Player to a user Developer, where developer extends Player

I have a class Player - extending ApplicationUser - and Developer - extending Player -. I want the users to be able to become developers whenever they wish, so I would have to somehow convert an object of the class Player to one of the class Developer at some point, but I'm confused about how to actually do this.
public class Player : ApplicationUser
{
public virtual ICollection<Playing> Playing { get; set; }
}
public class Developer : Player
{
public DateTime DateConverted { get; set; }
public virtual ICollection<Game> Games { get; set; }
}
I though that I could retrieve the object Player, remove it from the database, instantiate a object of the type Developer, copying all information and save it. It would be something like this:
public virtual async Task<Developer> Convert(Player player)
{
var developer = new Developer
{
Id = player.Id,
DateBirth = player.DateBirth,
DateCreated = player.DateCreated,
DateUpdated = player.DateUpdated,
Email = player.Email,
EmailConfirmed = player.EmailConfirmed,
UserName = player.UserName
};
Db.Players.Remove(player);
Db.Developers.Add(developer);
}
The problem is: when I delete the entry of the player, all relationships get lost with it.
Is there a better/easier way to do this? As far as I know, I would only have to change the discriminator field in the ApplicationUser table to "developer".
Well, I haven't been able to fix this properly, but the only workaround that I've found was to not separate Player and Developer into two classes, but instead to keep both classes' properties in Player. The difference would be given only by the property DateCoverted, that represents the date in which a given Player was converted to a Developer.
Something like this:
public class Player : ApplicationUser
{
public virtual ICollection<Playing> Playing { get; set; }
public DateTime? DateConverted { get; set; }
public virtual ICollection<Game> Games { get; set; }
}
public class PlayerService : Service<Player>
{
public virtual async Task<Player> Convert(string id)
{
var developer = await Db.Players.FindAsync(id);
developer.DateConverted = DateTime.Now;
await Update(developer);
return developer;
}
}

I'm sorry, but it's another: Found shared references to a collection

Recently I've been adding some features to my local project, and I'm struggling with this one part. The short of it is NHibernate give me the line:
Found shared references to a collection: Page.Menus
The simple part of it is, I only want to save the relational map that binds menus to pages, which you can see the Reference below in PageMap. I should add that loading data works great, it's the saving that's killing me.
I spent a lot of time yesterday digging through here, and the good ole web, trying to find the answer, and I just kept striking out. Maybe it's bad searching on my part, but I feel I tried everything. If you know where it is can you please supply it? (thanks)
For the actual details, I've tried to simplify what's going on. I've added the PageReposity, UnitOfWork, and the proxy objects, as well as their mappings.
Where I get hazy on is the cascade, and how to save the relationship table (many to many)
For the first part, here is what happens when I save (Add). I've actually done this a few ways within the PageRepository. Since I'm strugging with the Add(), I've included it here:
public override bool Add(Page entity)
{
UnitOfWork.Save(entity);
/* I have also tried doing the following below, which doesn't help
for (var index = 0; index < entity.Menus.Count; index++)
{
UnitOfWork.Save(entity.Menus[index]);
}
*/
UnitOfWork.Commit(); // bam, error!
return true;
}
In the UnitOfWork I've setup the following in the ctor (the ISession is injected each time via ninject like so:
// DomainModule
...
Bind<ISFactory>().To<NHibernateSessionFactory>()
.InSingletonScope()
.WithConstructorArgument("connectionString", _connectWeb);
...
// Back to the UnitOfWork
...
private ISession Session { get; set; }
...
public UnitOfWork(ISFactory sessionFactory)
{
_sessionFactory = sessionFactory.GetSessionFactory();
Session = _sessionFactory.OpenSession();
Session.FlushMode = FlushMode.Never; // I have also tried FlushMode.Commit
_transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
}
...
public void Save(object obj)
{
Session.Save(obj);
}
...
public void Commit()
{
if (!_transaction.IsActive)
{
throw new InvalidOperationException("Oops! We don't have an active transaction");
}
try
{
_transaction.Commit();
Session.Flush(); // I did this FlushMode.Never was set
}
catch (Exception exception)
{
_transaction.Rollback();
throw;
}
}
I've got 3 classes here:
Page, Menu, and Link.
public class Page : IEntity<int>
{
public virtual int Id { get; set; }
public virtual IList<Menu> Menus { get; set; }
}
public class Menu : IEntity<int>
{
public virtual int Id { get; set; }
public virtual IList<Link> Links { get; set; }
}
public class Link : IEntity<int>
{
public virtual int Id { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual string Url { get; set; }
}
Then I also have a Mappings:
public class PageMap : ClassMap<Page>
{
public PageMap()
{
Id(x => x.Id).GeneratedBy.Native();
HasManyToMany(x => x.Menus)
.Table("MenuToPage")
.ParentKeyColumn("FkPageId")
.ChildKeyColumn("FkMenuId").Cascade.SaveUpdate(); // the cascade is new here just trying to see if it helps
}
}
public class MenuMap : ClassMap<Menu>
{
public MenuMap()
{
Id(x => x.Id); // I had .GeneratedBy.Native(); attached here too.
HasManyToMany(x => x.Links)
.Table("MenuToLinks")
.ChildKeyColumn("FkLinksId")
.ParentKeyColumn("FkMenuId")
.OrderBy("MenuOrder ASC")
.Not.LazyLoad()
.Cascade.None(); // the cascade is new here just trying to see if it helps
}
}
public class LinkMap : ClassMap<Link>
{
public LinkMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Url);
Map(x => x.CreatedDate);
Map(x => x.ModifiedDate);
References(x => x.MetaData, "FkMetaDataId").Not.Nullable().Not.LazyLoad();
}
}
Can anyone help me or point me in a direction, I'd really appreciate your help.
Like always thank you,
Kelly
Unfortunately you have posted everything but the construction of your objects before you safe them.
Usually this error can occur if you assign the same collection of entities to different instances. For example (pseudo code)
var menuList = new List<Menu>();...
pageA.Menus = menuList;
pageB.Menus = menuList;
This would set the reference of menuList to both, pageA.Menus and pageB.Menus.
Instead, assign all items of menuList to each page with pageA.AddRange(menuList) or a loop or whatever...

MVC 4 EF5 - Add sub-entities error

I am getting an Object reference not set to an instance of an object error when trying to add multiple entity levels to my EF context.
Take the following three-level example class structure:
public class Forum
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Blog> Blogs { get; set; }
}
public class Blog
{
public int ID { get; set; }
public string Name { get; set; }
public int ForumID { get; set; }
public virtual Forum Forum { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int ID { get; set; }
public string Name { get; set; }
public int BlogID { get; set; }
public virtual Blog Blog { get; set; }
}
For a given Forum, I want to add a new Blog with a new Post:
Forum MyForum = context.Forums.Find(1);
Blog MyBlog = new Blog { Name = "My New Blog" };
Post MyPost = new Post { Name = "My New Post" };
MyForum.Blogs.Add(MyBlog); // This WORKS
MyBlog.Posts.Add(MyPost); // This FAILS
context.SaveChanges(); // We never make it this far
I've tried every possible order combination, including placing context.SaveChanges() immediately after .Add(MyBlog). It seems like it's choking because there is no Blog.ID to use for Post.BlogID, but EF generates temporary key values for use in this situation.
Any ideas?
Hints at the answer (and the root problem) can be found at:
Entity Framework Uninitialised Collection
Entity Framework 4.1 Code First - Should many relationship ICollections be initialised
The "simple" solution is to manually initialize the Blog.Posts collection:
Blog MyBlog = new Blog { Name = "My New Post", Posts = new List<Post>() };
Alternatively, you can build this logic into the class constructor as recommended by Ladislav in the second link.
Basically, when you create a new object, the collection is null and not initialized as a List<>, so the .Add() call fails. The Forum.Blogs collection is able to lazy-load because it derives from the database context. Blog.Posts, however, is created from scratch, and EF can't help you, so the collection is null by default.

SimpleRepository and booleans with SubSonic

I am trying to add a boolean column in SubSonic 3.0.0.3 and without this column the code works fine but as soon as I had a bool variable into my model this fails with the following error:
The name "False" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Anyonw know if this should be supported and if it is what I may be doing wrong:
Data Object Class:
public class Desk
{
[SubSonicPrimaryKey]
public int DeskId { get; set; }
public string DeskName { get; set; }
public string SAPCode { get; set; }
public int LocationId { get; set; }
public bool Active { get; set; }
}
Use of Class:
var d = new Desk();
d.DeskName = "Test";
d.SAPCode = "12345";
d.LocationId = 2;
d.Active = true;
var repository = new SimpleRepository("SubSonicTesting", SimpleRepositoryOptions.RunMigrations);
repository.Add(d);
I have faced the exact same issue (version 3.0.0.3) when I added a bool property named "IsAccountOwner". The problem seems to be with migrations because when I deleted the table, SubSonic re-created it correctly with the added column.
I'm using SQL Server 2008 Express, in case that matters. The error is related to a malformed query perhaps?

Resources