0
votes

I am using Visual Studio and I'm new to it. I am trying to do the following:

  • The user logs in and clicks a page called "subjects"
  • Check the user is logged in (if not, display "you must be logged in..")
  • If the user is logged in, obtain their information from the Subjects table (I should note my tables are not structured "properly", as at this time I am just testing/learning a few things).
  • If in the Subjects table under "tutor" the value is 0 - return page with message "List of the subjects you take", otherwise, return "List of the subjects you teach".

I've been working on this for hours and I keep getting errors. Mainly I think I don't understand how User.Identity.GetUserId(); works, because I am trying to compare the value it returns to the userID in the subjects table, because I assumed it gets the ID from the AspNetUsers table, so I tried to match them. I am getting an error when I try to access the subjects page: "Additional information: DbComparisonExpression requires arguments with comparable types." They are both the same type (nvarchar) and before this they were different data types and I kept having problems with comparisons which is why I changed them, to test it.

So anyway I think the problem is I don't know how User.Identity.GetUserId(); works, what exactly it returns. I was working on the assumption it returns the UserId from AspNetUsers table, not sure why, but probably why I get comparison errors.

This is part of my code:

 SubjectsContext1 db = new SubjectsContext1(); 

            if (User.Identity.IsAuthenticated) { //check if user is logged in


                var userInfo = User.Identity.GetUserId(); //get logged in user info

                //find logged in user info from Subjects table (errors here)

                var userData = (from c in db.Subjects
                            where c.studentID.Equals(userInfo) 
                            select c).FirstOrDefault();
                var email = userData.email;
                var tutorStatus = userData.tutor;


               if(tutorStatus == 0)
                {
                    ViewBag.Message = "A list of the subjects you take.";
                    return View();
                }
                else
                {
                    ViewBag.Message = "A list of the subjects you teach.";
                    return View();
                }

            }
            else
            {
                ViewBag.Message = "You must be logged in to view this page!";
                return View();
            }

I just want to learn how I can get the logged in user's information to get their information from the Subjects table I made.

1
User.Identity.GetUserId() as its name suggests returns the UserId of logged in user. What is the error exactly?Ali Baig
In your subject table, your StudentId should be of type string (nvarchar) coming from AspNetUsers table, if that's the case then you should see no errorsAli Baig
I get this error: "An exception of type 'System.ArgumentException' occurred in EntityFramework.SqlServer.dll but was not handled in user code Additional information: DbComparisonExpression requires arguments with comparable types." Do I need to link the tables, and if yes, how may I do that?bettafish
What is the type of StudentId in your Student table, is it string (or nvarchar in sql)?Ali Baig
in the sql table it is nvarchar(128)bettafish

1 Answers

2
votes

I don't know how User.Identity.GetUserId(); works, what exactly it returns

It returns the user id of logged in user from your identity's user table (dbo.AspNetUsers table in your case) which is string.

Make sure you are using the same data type (string) for StudentId in your dbo.Students table, reconfirm that from your "Table Design" in SQL Server.