Click here to Skip to main content
15,923,222 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
When a user logs in i want them to see only db entry that match there id on the index page

im currently using the below which works as fare as getting the correct data but get a error when i run the project

Code:
SQL
public ActionResult Index()
{
    return View(db.UserProfiles.ToList().Where(u => u.UserId == (int)Membership.GetUser().ProviderUserKey));
}



Error:
nullreferenceexception was unhandled by the user code
Object reference not set to an instance of an object.
Posted
Updated 24-Sep-13 4:23am
v2

To fully implement authorization in MVC4 you have to do a little more work then that. I found (and used) a great article for this problem :
http://primaryobjects.com/CMS/Article147.aspx[^]

Works like a charm and helps you through step by step.

Good luck!
 
Share this answer
 
OK, so break that line down and start assigning each piece to a variable so you can see what each piece is returning. Either db.UserProfiles is returning null, or MemberShip.GetUser() is returning null.
 
Share this answer
 
Comments
Adam Jones 24-Sep-13 4:02am    
Membership.GetUser().ProviderUserKey is returning the ID
db.UserProfile is return the Select query string
Dave Kreskowiak 24-Sep-13 8:34am    
http://forums.asp.net/t/939408.aspx

And if that doesn't help you, read these: https://www.google.com/#q=asp.net+membership.getuser+returns+null
Adam Jones 24-Sep-13 9:47am    
Can i note this only happens when i stop and start the project.
I need to log out and back in to get it working with no error
Reason for the is issue was not have [Authorize] above the index call methode

Thanks for all your help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900