0

I am trying to implement simple membership in my application. My problem is that I want to be able to display the data in the userprofile table for the current user but I dont know how to select it from the DB

I have tried this but I am getting an error:

    UserProfile UserP = new UserProfile();

        ViewBag.Message = User.Identity.Name;
        return View();

        UserP = (from r in up.UserName
                  where up.UserName == User.Identity.Name.ToString()
                  select r).ToList().FirstOrDefault();


        return View(UserP);

Here is the error:

            Error   1   Cannot implicitly convert type 'char' to 'MvcApplication5.Models.UserProfile'   C:\Users\user\Desktop\MvcApplication5\MvcApplication5\Controllers\HomeController.cs 31  32  MvcApplication5

2 Answers2

0

If I got you right (your code is little bit broken, it has two returns, so I assume there is just two pieces of code), try this:

UserP = (from r in up
                  where up.UserName == User.Identity.Name.ToString()
                  select r).FirstOrDefault();

Just get rid of up.UserName in your query. ToList() is also not needed.

P.S. For the future:

I also suggest you adding another column called LoweredUserName and perform checking in the following way:

where up.LoweredUserName == User.Identity.Name.ToString().ToLower()
Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58
  • Hi Roman, Really grateful for the response, but in this case what is "up"? Usually it would refer to the DBContext am I correct? But when I tried to run ADO.Entity Model it created errors because of the duplicated classes so I dont know what "up" should be??? Sorry I didnt make it very clear –  Dec 09 '13 at 14:57
  • Could you please provide more code of your application? Thanks! – Roman Pushkin Dec 09 '13 at 15:01
  • Sorrym I dont really have any code to give you yet. What I just really need to know is what DBContext do i use to access the UserProfile table data??? –  Dec 09 '13 at 15:07
  • Actually, yes. You have to use DbContext. It should look like this: var userProfile = context.UserProfiles.Where(up => up.UserName == User.Identity.Name.ToString()).FirstOrDefault(); – Roman Pushkin Dec 09 '13 at 15:09
  • What is "context" and "up" you have above? Just to state again I know how to access table data normally with the ADO.NET Model creator. But not with these Simple membership tables.. Sorry if this is a stupid question –  Dec 09 '13 at 15:18
  • Ah, you don't even have context. Okay, if you know how to access table data normally, just look at my previous response to other topic. Just put your logic to ValidatePassword: http://stackoverflow.com/questions/20454888/custom-database-with-asp-net-membership-and-authorisation/20455544#20455544 PS I don't like SimpleMembership, it's easier to make your own table and perform custom validation. – Roman Pushkin Dec 09 '13 at 15:21
  • Thanks Roman. Im beginning to think the same and I think I will create a custom login. But since ive spent so long on it, just out of interest id like to know how to do it this way –  Dec 09 '13 at 17:01
0

Here is how you can access the UserProfile.

var context = new UsersContext();
var username = User.Identity.Name;
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
return View(user);

For more on customizing the UserProfile and accessing it read this article.

Kevin Junghans
  • 17,475
  • 4
  • 45
  • 62
  • Hi Kevin. Thanks for the reply I tried your code there but I recieved an error. Visual studio gave me this error: An exception occurred while initializing the database. See the InnerException for details. Cannot attach the file 'C:\Users\user\Desktop\MvcApplication5\MvcApplication5\App_Data\aspnet-MvcApplication5-20131208163103.mdf' as database 'aspnet-MvcApplication5-20131208163103' –  Dec 09 '13 at 15:29
  • This error is not specific to the code. EF is having troubles connecting to the database you have configured for SimpleMembership. Try this QA. [http://stackoverflow.com/questions/17012839/cannot-attach-the-file-mdf-as-database] – Kevin Junghans Dec 09 '13 at 18:10